This commit is contained in:
Mikhail 2023-06-28 15:08:09 +08:00
parent cadf7abbd3
commit 5b3bce5476
5 changed files with 98 additions and 87 deletions

View File

@ -13,7 +13,16 @@ namespace DCFApixels.DragonECS
foreach (var item in _components)
item.Add(world, entityID);
}
private void OnValidate()
{
if (_components == null) return;
foreach (var item in _components)
{
if (item is ITemplateComponentOnValidate g)
g.OnValidate(gameObject);
}
}
private void OnDrawGizmos()
{
if (_components == null) return;

View File

@ -23,6 +23,10 @@ namespace DCFApixels.DragonECS
Selected
}
}
public interface ITemplateComponentOnValidate
{
public void OnValidate(GameObject gameObject);
}
[Serializable]
public abstract class TemplateComponentInitializerBase
@ -73,7 +77,7 @@ namespace DCFApixels.DragonECS
#endregion
}
[Serializable]
public abstract class TemplateComponentInitializer<T> : TemplateComponentInitializerBase, ITemplateComponentName, ITemplateComponentGizmos
public abstract class TemplateComponentInitializer<T> : TemplateComponentInitializerBase, ITemplateComponentName, ITemplateComponentGizmos, ITemplateComponentOnValidate
{
private static string _autoname = GetName(typeof(T));
private static Color _autoColor = GetColor(typeof(T));
@ -94,6 +98,7 @@ namespace DCFApixels.DragonECS
public abstract void Add(EcsWorld w, int e);
public virtual void OnGizmos(Transform transform, ITemplateComponentGizmos.Mode mode) { }
public virtual void OnValidate(GameObject gameObject) { }
}
internal static class ITemplateBrowsableExt

View File

@ -78,75 +78,75 @@ namespace DCFApixels.DragonECS
t.Apply(_world, entityID);
}
}
}
#if UNITY_EDITOR
namespace Editors
namespace DCFApixels.DragonECS.Editors
{
using System.Collections.Generic;
using UnityEditor;
[CustomEditor(typeof(EcsEntityConnect))]
public class EcsEntityEditor : Editor
{
using System.Collections.Generic;
using UnityEditor;
private EcsEntityConnect Target => (EcsEntityConnect)target;
private GUIStyle _greenStyle;
private GUIStyle _redStyle;
[CustomEditor(typeof(EcsEntityConnect))]
public class EcsEntityEditor : Editor
private bool _isInit = false;
private void Init()
{
private EcsEntityConnect Target => (EcsEntityConnect)target;
private GUIStyle _greenStyle;
private GUIStyle _redStyle;
if (_isInit)
return;
_greenStyle = EcsEditor.GetStyle(new Color32(75, 255, 0, 100));
_redStyle = EcsEditor.GetStyle(new Color32(255, 0, 75, 100));
private bool _isInit = false;
_isInit = true;
}
private void Init()
public override void OnInspectorGUI()
{
Init();
if (Target.IsAlive)
GUILayout.Box("Connected", _greenStyle, GUILayout.ExpandWidth(true));
else
GUILayout.Box("Not connected", _redStyle, GUILayout.ExpandWidth(true));
if (Target.Entity.TryGetID(out int id))
EditorGUILayout.IntField(id);
else
EditorGUILayout.IntField(0);
GUILayout.Label(Target.Entity.ToString());
base.OnInspectorGUI();
if (GUILayout.Button("Autoset Templates"))
{
if (_isInit)
return;
Target.SetTemplates_Editor(Target.GetComponents<EntityTemplate>());
_greenStyle = EcsEditor.GetStyle(new Color32(75, 255, 0, 100));
_redStyle = EcsEditor.GetStyle(new Color32(255, 0, 75, 100));
_isInit = true;
EditorUtility.SetDirty(target);
}
if (GUILayout.Button("Autoset Templates Cascade"))
{
foreach (var item in Target.GetComponentsInChildren<EcsEntityConnect>())
{
item.SetTemplates_Editor(item.GetComponents<EntityTemplate>());
EditorUtility.SetDirty(item);
}
}
public override void OnInspectorGUI()
if (Target.IsAlive)
{
Init();
if (Target.IsAlive)
GUILayout.Box("Connected", _greenStyle, GUILayout.ExpandWidth(true));
else
GUILayout.Box("Not connected", _redStyle, GUILayout.ExpandWidth(true));
if (Target.Entity.TryGetID(out int id))
EditorGUILayout.IntField(id);
else
EditorGUILayout.IntField(0);
GUILayout.Label(Target.Entity.ToString());
base.OnInspectorGUI();
if (GUILayout.Button("Autoset Templates"))
{
Target.SetTemplates_Editor(Target.GetComponents<EntityTemplate>());
EditorUtility.SetDirty(target);
}
if (GUILayout.Button("Autoset Templates Cascade"))
{
foreach (var item in Target.GetComponentsInChildren<EcsEntityConnect>())
{
item.SetTemplates_Editor(item.GetComponents<EntityTemplate>());
EditorUtility.SetDirty(item);
}
}
if (Target.IsAlive)
{
List<object> comps = new List<object>();
Target.World.GetComponents(Target.Entity.ID, comps);
GUILayout.TextArea(string.Join("\r\n", comps));
}
List<object> comps = new List<object>();
Target.World.GetComponents(Target.Entity.ID, comps);
GUILayout.TextArea(string.Join("\r\n", comps));
}
}
}
#endif
}
#endif

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: cdc92b01ccc1e684f955830aa7cea7d4
guid: 495156623a7b1e94087f916ba42745e6
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -2,97 +2,94 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;
namespace DCFApixels.DragonECS
{
[Serializable]
[DebugColor(255 / 3, 255, 0)]
public struct UnityComponent<T> : IEcsComponent, IEnumerable<T>//IntelliSense hack
where T : class
where T : Component
{
public T obj;
IEnumerator<T> IEnumerable<T>.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack
}
[Serializable]
[MovedFrom(false, "Client", null, "RefRigitBodyInitializer")]
public sealed class UnityComponentRigitBodyInitializer : TemplateComponentInitializer<UnityComponent<Rigidbody>>
public class UnityComponentInitializer<T> : TemplateComponentInitializer<UnityComponent<T>> where T : Component
{
public override string Name => "UnityComponent/" + typeof(T).Name;
public sealed override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<T>>().Add(e) = component;
public override void OnValidate(GameObject gameObject)
{
if (component.obj == null)
component.obj = gameObject.GetComponent<T>();
}
}
[Serializable]
public sealed class UnityComponentRigitBodyInitializer : UnityComponentInitializer<Rigidbody>
{
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<Rigidbody>>().Add(e) = component;
}
[Serializable]
[MovedFrom(false, "Client", null, "RefAnimatorInitializer")]
public sealed class UnityComponentAnimatorInitializer : TemplateComponentInitializer<UnityComponent<Animator>>
public sealed class UnityComponentAnimatorInitializer : UnityComponentInitializer<Animator>
{
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<Animator>>().Add(e) = component;
}
[Serializable]
public sealed class UnityComponentCharacterControllerInitializer : TemplateComponentInitializer<UnityComponent<CharacterController>>
public sealed class UnityComponentCharacterControllerInitializer : UnityComponentInitializer<CharacterController>
{
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<CharacterController>>().Add(e) = component;
}
#region Colliders
[Serializable]
public sealed class UnityComponentColliderInitializer : TemplateComponentInitializer<UnityComponent<Collider>>
public sealed class UnityComponentColliderInitializer : UnityComponentInitializer<Collider>
{
public override string Name => "UnityComponent/Collider/" + nameof(Collider);
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<Collider>>().Add(e) = component;
}
[Serializable]
public sealed class UnityComponentBoxColliderInitializer : TemplateComponentInitializer<UnityComponent<BoxCollider>>
public sealed class UnityComponentBoxColliderInitializer : UnityComponentInitializer<BoxCollider>
{
public override string Name => "UnityComponent/Collider/" + nameof(BoxCollider);
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<BoxCollider>>().Add(e) = component;
}
[Serializable]
public sealed class UnityComponentSphereColliderInitializer : TemplateComponentInitializer<UnityComponent<SphereCollider>>
public sealed class UnityComponentSphereColliderInitializer : UnityComponentInitializer<SphereCollider>
{
public override string Name => "UnityComponent/Collider/" + nameof(SphereCollider);
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<SphereCollider>>().Add(e) = component;
}
[Serializable]
public sealed class UnityComponentCapsuleColliderInitializer : TemplateComponentInitializer<UnityComponent<CapsuleCollider>>
public sealed class UnityComponentCapsuleColliderInitializer : UnityComponentInitializer<CapsuleCollider>
{
public override string Name => "UnityComponent/Collider/" + nameof(CapsuleCollider);
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<CapsuleCollider>>().Add(e) = component;
}
[Serializable]
public sealed class UnityComponentMeshColliderInitializer : TemplateComponentInitializer<UnityComponent<MeshCollider>>
public sealed class UnityComponentMeshColliderInitializer : UnityComponentInitializer<MeshCollider>
{
public override string Name => "UnityComponent/Collider/" + nameof(MeshCollider);
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<MeshCollider>>().Add(e) = component;
}
#endregion
#region Joints
[Serializable]
public sealed class UnityComponentJointInitializer : TemplateComponentInitializer<UnityComponent<Joint>>
public sealed class UnityComponentJointInitializer : UnityComponentInitializer<Joint>
{
public override string Name => "UnityComponent/Joint/" + nameof(Joint);
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<Joint>>().Add(e) = component;
}
[Serializable]
public sealed class UnityComponentFixedJointInitializer : TemplateComponentInitializer<UnityComponent<FixedJoint>>
public sealed class UnityComponentFixedJointInitializer : UnityComponentInitializer<FixedJoint>
{
public override string Name => "UnityComponent/Joint/" + nameof(FixedJoint);
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<FixedJoint>>().Add(e) = component;
}
[Serializable]
public sealed class UnityComponentCharacterJointInitializer : TemplateComponentInitializer<UnityComponent<CharacterJoint>>
public sealed class UnityComponentCharacterJointInitializer : UnityComponentInitializer<CharacterJoint>
{
public override string Name => "UnityComponent/Joint/" + nameof(CharacterJoint);
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<CharacterJoint>>().Add(e) = component;
}
[Serializable]
public sealed class UnityComponentConfigurableJointInitializer : TemplateComponentInitializer<UnityComponent<ConfigurableJoint>>
public sealed class UnityComponentConfigurableJointInitializer : UnityComponentInitializer<ConfigurableJoint>
{
public override string Name => "UnityComponent/Joint/" + nameof(ConfigurableJoint);
public override void Add(EcsWorld w, int e) => w.GetPool<UnityComponent<ConfigurableJoint>>().Add(e) = component;
}
#endregion
}