From a2340ae3c3a8742bb1b14777284623cb042f55b5 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sat, 9 Mar 2024 03:35:01 +0800 Subject: [PATCH] update --- src/Buildin/UnityComponents.cs | 10 +- src/Buildin/UnityGameCyclieProcesses.cs | 244 +++++++++--------- src/Connectors/EcsEntityConnect.cs | 63 +++-- .../Editor/EcsEntityConnectEditor.cs | 40 ++- .../Editor/EntityTemplateEditor.cs | 34 +-- src/Internal/Editor/EcsGUI.cs | 80 ++++-- 6 files changed, 284 insertions(+), 187 deletions(-) diff --git a/src/Buildin/UnityComponents.cs b/src/Buildin/UnityComponents.cs index aa79743..57f5e73 100644 --- a/src/Buildin/UnityComponents.cs +++ b/src/Buildin/UnityComponents.cs @@ -22,8 +22,14 @@ namespace DCFApixels.DragonECS { this.obj = obj; } - IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack - IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack + IEnumerator IEnumerable.GetEnumerator() //IntelliSense hack + { + throw new NotImplementedException(); + } + IEnumerator IEnumerable.GetEnumerator() //IntelliSense hack + { + throw new NotImplementedException(); + } } #region Unity Component Templates diff --git a/src/Buildin/UnityGameCyclieProcesses.cs b/src/Buildin/UnityGameCyclieProcesses.cs index aa086c1..12a1f0e 100644 --- a/src/Buildin/UnityGameCyclieProcesses.cs +++ b/src/Buildin/UnityGameCyclieProcesses.cs @@ -1,139 +1,141 @@ -using DCFApixels.DragonECS.Internal; -using DCFApixels.DragonECS.RunnersCore; +using DCFApixels.DragonECS.RunnersCore; +using DCFApixels.DragonECS.Unity.Internal; namespace DCFApixels.DragonECS { + [MetaName(nameof(DrawGizmos))] + [MetaColor(MetaColor.Orange)] public interface IEcsGizmosProcess : IEcsProcess { - public void DrawGizmos(EcsPipeline pipeline); + public void DrawGizmos(); } - public static class IEcsGizmosProcessExtensions - { - public static void DrawGizmos(this EcsPipeline systems) - { - systems.GetRunnerInstance().DrawGizmos(systems); - } - } - + [MetaName(nameof(LateRun))] + [MetaColor(MetaColor.Orange)] public interface IEcsLateRunProcess : IEcsProcess { - public void LateRun(EcsPipeline pipeline); - } - public static class IEcsLateRunSystemExtensions - { - public static void LateRun(this EcsPipeline pipeline) - { - pipeline.GetRunnerInstance().LateRun(pipeline); - } + public void LateRun(); } + [MetaName(nameof(FixedRun))] + [MetaColor(MetaColor.Orange)] public interface IEcsFixedRunProcess : IEcsProcess { - public void FixedRun(EcsPipeline pipeline); + public void FixedRun(); } - public static class IEcsFixedRunSystemExtensions + + public static class UnityProcessExtensions { + public static void DrawGizmos(this EcsPipeline pipeline) + { + pipeline.GetRunnerInstance().DrawGizmos(); + } + public static void LateRun(this EcsPipeline pipeline) + { + pipeline.GetRunnerInstance().LateRun(); + } public static void FixedRun(this EcsPipeline pipeline) { - pipeline.GetRunnerInstance().FixedRun(pipeline); - } - } - - namespace Internal - { - [MetaColor(MetaColor.Orange)] - public class EcsLateGizmosRunner : EcsRunner, IEcsGizmosProcess - { -#if DEBUG && !DISABLE_DEBUG - private EcsProfilerMarker[] _markers; -#endif - public void DrawGizmos(EcsPipeline pipeline) - { -#if DEBUG && !DISABLE_DEBUG - for (int i = 0; i < Process.Length; i++) - { - using (_markers[i].Auto()) - Process[i].DrawGizmos(pipeline); - } -#else - foreach (var item in targets) item.DrawGizmos(pipeline); -#endif - } - -#if DEBUG && !DISABLE_DEBUG - protected override void OnSetup() - { - _markers = new EcsProfilerMarker[Process.Length]; - for (int i = 0; i < Process.Length; i++) - { - _markers[i] = new EcsProfilerMarker($"{Process[i].GetType().Name}.{nameof(DrawGizmos)}"); - } - } -#endif - } - - [MetaColor(MetaColor.Orange)] - public class EcsLateRunRunner : EcsRunner, IEcsLateRunProcess - { -#if DEBUG && !DISABLE_DEBUG - private EcsProfilerMarker[] _markers; -#endif - public void LateRun(EcsPipeline pipeline) - { -#if DEBUG && !DISABLE_DEBUG - for (int i = 0; i < Process.Length; i++) - { - using (_markers[i].Auto()) - { - Process[i].LateRun(pipeline); - } - } -#else - foreach (var item in targets) item.LateRun(pipeline); -#endif - } - -#if DEBUG && !DISABLE_DEBUG - protected override void OnSetup() - { - _markers = new EcsProfilerMarker[Process.Length]; - for (int i = 0; i < Process.Length; i++) - { - _markers[i] = new EcsProfilerMarker($"EcsRunner.{Process[i].GetType().Name}.{nameof(LateRun)}"); - } - } -#endif - } - [MetaColor(MetaColor.Orange)] - public class EcsFixedRunRunner : EcsRunner, IEcsFixedRunProcess - { -#if DEBUG && !DISABLE_DEBUG - private EcsProfilerMarker[] _markers; -#endif - public void FixedRun(EcsPipeline pipeline) - { -#if DEBUG && !DISABLE_DEBUG - for (int i = 0; i < Process.Length; i++) - { - using (_markers[i].Auto()) - { - Process[i].FixedRun(pipeline); - } - } -#else - foreach (var item in targets) item.FixedRun(pipeline); -#endif - } - -#if DEBUG && !DISABLE_DEBUG - protected override void OnSetup() - { - _markers = new EcsProfilerMarker[Process.Length]; - for (int i = 0; i < Process.Length; i++) - { - _markers[i] = new EcsProfilerMarker($"EcsRunner.{Process[i].GetType().Name}.{nameof(FixedRun)}"); - } - } -#endif + pipeline.GetRunnerInstance().FixedRun(); } } } +namespace DCFApixels.DragonECS.Unity.Internal +{ + + [MetaColor(MetaColor.Orange)] + public class EcsLateGizmosRunner : EcsRunner, IEcsGizmosProcess + { +#if DEBUG && !DISABLE_DEBUG + private EcsProfilerMarker[] _markers; +#endif + public void DrawGizmos() + { +#if DEBUG && !DISABLE_DEBUG + for (int i = 0; i < Process.Length; i++) + { + using (_markers[i].Auto()) + { + Process[i].DrawGizmos(); + } + } +#else + foreach (var item in targets) item.DrawGizmos(); +#endif + } + +#if DEBUG && !DISABLE_DEBUG + protected override void OnSetup() + { + _markers = new EcsProfilerMarker[Process.Length]; + for (int i = 0; i < Process.Length; i++) + { + _markers[i] = new EcsProfilerMarker($"{Process[i].GetType().Name}.{nameof(DrawGizmos)}"); + } + } +#endif + } + + [MetaColor(MetaColor.Orange)] + public class EcsLateRunRunner : EcsRunner, IEcsLateRunProcess + { +#if DEBUG && !DISABLE_DEBUG + private EcsProfilerMarker[] _markers; +#endif + public void LateRun() + { +#if DEBUG && !DISABLE_DEBUG + for (int i = 0; i < Process.Length; i++) + { + using (_markers[i].Auto()) + { + Process[i].LateRun(); + } + } +#else + foreach (var item in targets) item.LateRun(); +#endif + } + +#if DEBUG && !DISABLE_DEBUG + protected override void OnSetup() + { + _markers = new EcsProfilerMarker[Process.Length]; + for (int i = 0; i < Process.Length; i++) + { + _markers[i] = new EcsProfilerMarker($"EcsRunner.{Process[i].GetType().Name}.{nameof(LateRun)}"); + } + } +#endif + } + [MetaColor(MetaColor.Orange)] + public class EcsFixedRunRunner : EcsRunner, IEcsFixedRunProcess + { +#if DEBUG && !DISABLE_DEBUG + private EcsProfilerMarker[] _markers; +#endif + public void FixedRun() + { +#if DEBUG && !DISABLE_DEBUG + for (int i = 0; i < Process.Length; i++) + { + using (_markers[i].Auto()) + { + Process[i].FixedRun(); + } + } +#else + foreach (var item in targets) item.FixedRun(); +#endif + } + +#if DEBUG && !DISABLE_DEBUG + protected override void OnSetup() + { + _markers = new EcsProfilerMarker[Process.Length]; + for (int i = 0; i < Process.Length; i++) + { + _markers[i] = new EcsProfilerMarker($"EcsRunner.{Process[i].GetType().Name}.{nameof(FixedRun)}"); + } + } +#endif + } +} diff --git a/src/Connectors/EcsEntityConnect.cs b/src/Connectors/EcsEntityConnect.cs index 6974007..f12313f 100644 --- a/src/Connectors/EcsEntityConnect.cs +++ b/src/Connectors/EcsEntityConnect.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Linq; using System.Runtime.CompilerServices; using UnityEngine; @@ -7,8 +9,8 @@ namespace DCFApixels.DragonECS { private sealed class Aspect : EcsAspect { - public readonly EcsPool unityGameObjects; - public Aspect(Builder b) + public EcsPool unityGameObjects; + protected override void Init(Builder b) { unityGameObjects = b.Include(); } @@ -22,16 +24,11 @@ namespace DCFApixels.DragonECS [SerializeField] private MonoEntityTemplate[] _monoTemplates; - internal void SetTemplates_Editor(MonoEntityTemplate[] tempaltes) - { - _monoTemplates = tempaltes; - } - #region Properties public entlong Entity { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => _entity; + get { return _entity; } } public EcsWorld World { @@ -43,46 +40,74 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _entity.IsAlive; } + public IEnumerable ScriptableTemplates + { + get { return _scriptableTemplates; } + } + public IEnumerable MonoTemplates + { + get { return _monoTemplates; } + } + public IEnumerable AllTemplates + { + get { return ((IEnumerable)_scriptableTemplates).Concat(_monoTemplates); } + } #endregion + #region Connect public void ConnectWith(entlong entity, bool applyTemplates = false) { - if (_entity.TryGetID(out int oldE) && _world != null) + if (_entity.TryGetID(out int oldEntityID) && _world != null) { - var s = _world.GetAspect(); - s.unityGameObjects.Del(oldE); + var a = _world.GetAspect(); + a.unityGameObjects.Del(oldEntityID); } _world = null; - if (entity.TryGetID(out int newE)) + if (entity.TryGetID(out int newEntityID)) { _entity = entity; _world = _entity.World; - var s = _world.GetAspect(); - if (!s.unityGameObjects.Has(newE)) s.unityGameObjects.Add(newE) = new UnityGameObject(gameObject); - + var a = _world.GetAspect(); + if (!a.unityGameObjects.Has(newEntityID)) + { + a.unityGameObjects.Add(newEntityID) = new UnityGameObject(gameObject); + } if (applyTemplates) + { ApplyTemplates(); + } } else { _entity = entlong.NULL; } } + #endregion + + #region ApplyTemplates public void ApplyTemplates() { ApplyTemplatesFor(_entity.ID); } public void ApplyTemplatesFor(int entityID) { - foreach (var t in _scriptableTemplates) + foreach (var template in _scriptableTemplates) { - t.Apply(_world.id, entityID); + template.Apply(_world.id, entityID); } - foreach (var t in _monoTemplates) + foreach (var template in _monoTemplates) { - t.Apply(_world.id, entityID); + template.Apply(_world.id, entityID); } } + #endregion + + #region Editor + internal void SetTemplates_Editor(MonoEntityTemplate[] tempaltes) + { + _monoTemplates = tempaltes; + } + #endregion } } \ No newline at end of file diff --git a/src/Connectors/Editor/EcsEntityConnectEditor.cs b/src/Connectors/Editor/EcsEntityConnectEditor.cs index 72b4f81..491f731 100644 --- a/src/Connectors/Editor/EcsEntityConnectEditor.cs +++ b/src/Connectors/Editor/EcsEntityConnectEditor.cs @@ -1,7 +1,10 @@ #if UNITY_EDITOR using DCFApixels.DragonECS.Unity.Internal; +using System.Collections.Generic; +using System.Linq; using UnityEditor; using UnityEngine; +using static PlasticGui.LaunchDiffParameters; namespace DCFApixels.DragonECS.Unity.Editors { @@ -94,14 +97,39 @@ namespace DCFApixels.DragonECS.Unity.Editors private void DrawTemplates() { + EditorGUI.BeginChangeCheck(); var iterator = serializedObject.GetIterator(); iterator.NextVisible(true); - bool enterChildren = true; - while (iterator.NextVisible(enterChildren)) + while (iterator.NextVisible(false)) { EditorGUILayout.PropertyField(iterator, true); - enterChildren = false; } + if (EditorGUI.EndChangeCheck()) + { + serializedObject.ApplyModifiedProperties(); + } + } + + private void Autoset(EcsEntityConnect target) + { + var result = target.MonoTemplates.Where(o => o != null).Union(GetTemplatesFor(target.transform)); + + target.SetTemplates_Editor(result.ToArray()); + EditorUtility.SetDirty(target); + } + private IEnumerable GetTemplatesFor(Transform parent) + { + IEnumerable result = parent.GetComponents(); + for (int i = 0; i < parent.childCount; i++) + { + var child = parent.GetChild(i); + if (child.TryGetComponent(out _)) + { + return Enumerable.Empty(); + } + result = result.Concat(GetTemplatesFor(child)); + } + return result; } private void DrawButtons() @@ -109,15 +137,13 @@ namespace DCFApixels.DragonECS.Unity.Editors GUILayout.BeginHorizontal(); if (GUILayout.Button("Autoset")) { - Target.SetTemplates_Editor(Target.GetComponents()); - EditorUtility.SetDirty(target); + Autoset(Target); } if (GUILayout.Button("Autoset Cascade")) { foreach (var item in Target.GetComponentsInChildren()) { - item.SetTemplates_Editor(item.GetComponents()); - EditorUtility.SetDirty(item); + Autoset(item); } } GUILayout.EndHorizontal(); diff --git a/src/EntityTemplate/Editor/EntityTemplateEditor.cs b/src/EntityTemplate/Editor/EntityTemplateEditor.cs index 054fdba..6787868 100644 --- a/src/EntityTemplate/Editor/EntityTemplateEditor.cs +++ b/src/EntityTemplate/Editor/EntityTemplateEditor.cs @@ -107,43 +107,35 @@ namespace DCFApixels.DragonECS.Unity.Editors return; } - DrawTop(target); GUILayout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f)); + DrawTop(target); GUILayout.Label("", GUILayout.Height(0), GUILayout.ExpandWidth(true)); for (int i = 0; i < componentsProp.arraySize; i++) { DrawComponentData(componentsProp.GetArrayElementAtIndex(i), i); } GUILayout.EndVertical(); - DrawFooter(target); } private void DrawTop(ITemplateInternal target) { - if (GUILayout.Button("Add Component", GUILayout.Height(24f))) + switch (EcsGUI.Layout.AddClearComponentButtons()) { - Init(); - _genericMenu.ShowAsContext(); - } - } - private void DrawFooter(ITemplateInternal target) - { - if (GUILayout.Button("Clear", GUILayout.Height(24f))) - { - Init(); - serializedObject.FindProperty(target.ComponentsPropertyName).ClearArray(); - serializedObject.ApplyModifiedProperties(); + case EcsGUI.AddClearComponentButton.AddComponent: + Init(); + _genericMenu.ShowAsContext(); + break; + case EcsGUI.AddClearComponentButton.Clear: + Init(); + serializedObject.FindProperty(target.ComponentsPropertyName).ClearArray(); + serializedObject.ApplyModifiedProperties(); + break; } } + private void DrawComponentData(SerializedProperty componentRefProp, int index) { IComponentTemplate template = componentRefProp.managedReferenceValue as IComponentTemplate; - if (template == null) - { - DrawDamagedComponent(componentRefProp, index); - return; - } - - if (componentRefProp.managedReferenceValue == null) + if (template == null || componentRefProp.managedReferenceValue == null) { DrawDamagedComponent(componentRefProp, index); return; diff --git a/src/Internal/Editor/EcsGUI.cs b/src/Internal/Editor/EcsGUI.cs index 41d866b..aa4ebb9 100644 --- a/src/Internal/Editor/EcsGUI.cs +++ b/src/Internal/Editor/EcsGUI.cs @@ -4,6 +4,7 @@ using System; using System.Reflection; using UnityEditor; using UnityEngine; +using static UnityEngine.GraphicsBuffer; namespace DCFApixels.DragonECS.Unity.Editors { @@ -15,6 +16,25 @@ namespace DCFApixels.DragonECS.Unity.Editors private static readonly Rect RemoveButtonRect = new Rect(0f, 0f, 17f, 19f); private static readonly Rect TooltipIconRect = new Rect(0f, 0f, 21f, 15f); + + private static bool IsShowHidden + { + get { return DebugMonitorPrefs.instance.IsShowHidden; } + set { DebugMonitorPrefs.instance.IsShowHidden = value; } + } + private static bool IsShowRuntimeComponents + { + get { return DebugMonitorPrefs.instance.IsShowRuntimeComponents; } + set { DebugMonitorPrefs.instance.IsShowRuntimeComponents = value; } + } + + public enum AddClearComponentButton : byte + { + None = 0, + AddComponent, + Clear, + } + //private static GUILayoutOption[] _defaultParams; //private static bool _isInit = false; //private static void Init() @@ -27,17 +47,29 @@ namespace DCFApixels.DragonECS.Unity.Editors // _isInit = true; //} + public static AddClearComponentButton AddClearComponentButtons(Rect position) + { + //Rect rect = GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth, 36f); + position = RectUtility.AddPadding(position, 20f, 20f, 12f, 2f); + var (left, right) = RectUtility.HorizontalSliceLerp(position, 0.75f); + + if (GUI.Button(left, "Add Component")) + { + return AddClearComponentButton.AddComponent; + } + if (GUI.Button(right, "Clear")) + { + return AddClearComponentButton.Clear; + } + return AddClearComponentButton.None; + } + public static class Layout { - private static bool IsShowHidden + + public static AddClearComponentButton AddClearComponentButtons() { - get { return DebugMonitorPrefs.instance.IsShowHidden; } - set { DebugMonitorPrefs.instance.IsShowHidden = value; } - } - private static bool IsShowRuntimeComponents - { - get { return DebugMonitorPrefs.instance.IsShowRuntimeComponents; } - set { DebugMonitorPrefs.instance.IsShowRuntimeComponents = value; } + return EcsGUI.AddClearComponentButtons(GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth, 36f)); } public static void DrawRuntimeComponents(entlong entity, bool isWithFoldout = true) { @@ -59,8 +91,21 @@ namespace DCFApixels.DragonECS.Unity.Editors } if (isWithFoldout == false || IsShowRuntimeComponents) { + switch (EcsGUI.Layout.AddClearComponentButtons()) + { + case AddClearComponentButton.AddComponent: + GenericMenu genericMenu = RuntimeComponentsUtility.GetAddComponentGenericMenu(world); + RuntimeComponentsUtility.CurrentEntityID = entityID; + genericMenu.ShowAsContext(); + break; + case AddClearComponentButton.Clear: + break; + } + + GUILayout.Box("", UnityEditorUtility.GetStyle(GUI.color, 0.16f), GUILayout.ExpandWidth(true)); IsShowHidden = EditorGUI.Toggle(GUILayoutUtility.GetLastRect(), "Show Hidden", IsShowHidden); + foreach (var componentTypeID in componentTypeIDs) { var pool = world.GetPoolInstance(componentTypeID); @@ -70,13 +115,6 @@ namespace DCFApixels.DragonECS.Unity.Editors } } GUILayout.EndVertical(); - - if (GUILayout.Button("Add Component", GUILayout.Height(24f))) - { - GenericMenu genericMenu = RuntimeComponentsUtility.GetAddComponentGenericMenu(world); - RuntimeComponentsUtility.CurrentEntityID = entityID; - genericMenu.ShowAsContext(); - } } private static readonly BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private static void DrawRuntimeComponent(int entityID, IEcsPool pool) @@ -123,9 +161,17 @@ namespace DCFApixels.DragonECS.Unity.Editors } private static bool DrawRuntimeData(Type fieldType, GUIContent label, ExpandMatrix expandMatrix, object data, out object outData) - { - Type type = data.GetType(); + { + outData = data; + Type type = data == null ? typeof(void) : data.GetType(); + bool isUnityObject = typeof(UnityEngine.Object).IsAssignableFrom(fieldType); + + if (isUnityObject == false && data == null) + { + EditorGUILayout.TextField(label, "Null"); + return false; + } ref bool isExpanded = ref expandMatrix.Down(); bool changed = false; outData = data;