This commit is contained in:
Mikhail 2024-03-09 03:35:01 +08:00
parent cb4ef1c853
commit a2340ae3c3
6 changed files with 284 additions and 187 deletions

View File

@ -22,8 +22,14 @@ namespace DCFApixels.DragonECS
{ {
this.obj = obj; this.obj = obj;
} }
IEnumerator<T> IEnumerable<T>.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack IEnumerator<T> IEnumerable<T>.GetEnumerator() //IntelliSense hack
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); //IntelliSense hack {
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator() //IntelliSense hack
{
throw new NotImplementedException();
}
} }
#region Unity Component Templates #region Unity Component Templates

View File

@ -1,139 +1,141 @@
using DCFApixels.DragonECS.Internal; using DCFApixels.DragonECS.RunnersCore;
using DCFApixels.DragonECS.RunnersCore; using DCFApixels.DragonECS.Unity.Internal;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
[MetaName(nameof(DrawGizmos))]
[MetaColor(MetaColor.Orange)]
public interface IEcsGizmosProcess : IEcsProcess public interface IEcsGizmosProcess : IEcsProcess
{ {
public void DrawGizmos(EcsPipeline pipeline); public void DrawGizmos();
} }
public static class IEcsGizmosProcessExtensions [MetaName(nameof(LateRun))]
{ [MetaColor(MetaColor.Orange)]
public static void DrawGizmos(this EcsPipeline systems)
{
systems.GetRunnerInstance<EcsLateGizmosRunner>().DrawGizmos(systems);
}
}
public interface IEcsLateRunProcess : IEcsProcess public interface IEcsLateRunProcess : IEcsProcess
{ {
public void LateRun(EcsPipeline pipeline); public void LateRun();
}
public static class IEcsLateRunSystemExtensions
{
public static void LateRun(this EcsPipeline pipeline)
{
pipeline.GetRunnerInstance<EcsLateRunRunner>().LateRun(pipeline);
}
} }
[MetaName(nameof(FixedRun))]
[MetaColor(MetaColor.Orange)]
public interface IEcsFixedRunProcess : IEcsProcess 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<EcsLateGizmosRunner>().DrawGizmos();
}
public static void LateRun(this EcsPipeline pipeline)
{
pipeline.GetRunnerInstance<EcsLateRunRunner>().LateRun();
}
public static void FixedRun(this EcsPipeline pipeline) public static void FixedRun(this EcsPipeline pipeline)
{ {
pipeline.GetRunnerInstance<EcsFixedRunRunner>().FixedRun(pipeline); pipeline.GetRunnerInstance<EcsFixedRunRunner>().FixedRun();
}
}
namespace Internal
{
[MetaColor(MetaColor.Orange)]
public class EcsLateGizmosRunner : EcsRunner<IEcsGizmosProcess>, 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>, 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>, 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
} }
} }
} }
namespace DCFApixels.DragonECS.Unity.Internal
{
[MetaColor(MetaColor.Orange)]
public class EcsLateGizmosRunner : EcsRunner<IEcsGizmosProcess>, 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>, 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>, 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
}
}

View File

@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using UnityEngine; using UnityEngine;
@ -7,8 +9,8 @@ namespace DCFApixels.DragonECS
{ {
private sealed class Aspect : EcsAspect private sealed class Aspect : EcsAspect
{ {
public readonly EcsPool<UnityGameObject> unityGameObjects; public EcsPool<UnityGameObject> unityGameObjects;
public Aspect(Builder b) protected override void Init(Builder b)
{ {
unityGameObjects = b.Include<UnityGameObject>(); unityGameObjects = b.Include<UnityGameObject>();
} }
@ -22,16 +24,11 @@ namespace DCFApixels.DragonECS
[SerializeField] [SerializeField]
private MonoEntityTemplate[] _monoTemplates; private MonoEntityTemplate[] _monoTemplates;
internal void SetTemplates_Editor(MonoEntityTemplate[] tempaltes)
{
_monoTemplates = tempaltes;
}
#region Properties #region Properties
public entlong Entity public entlong Entity
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _entity; get { return _entity; }
} }
public EcsWorld World public EcsWorld World
{ {
@ -43,46 +40,74 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _entity.IsAlive; get => _entity.IsAlive;
} }
public IEnumerable<ScriptableEntityTemplate> ScriptableTemplates
{
get { return _scriptableTemplates; }
}
public IEnumerable<MonoEntityTemplate> MonoTemplates
{
get { return _monoTemplates; }
}
public IEnumerable<ITemplateInternal> AllTemplates
{
get { return ((IEnumerable<ITemplateInternal>)_scriptableTemplates).Concat(_monoTemplates); }
}
#endregion #endregion
#region Connect
public void ConnectWith(entlong entity, bool applyTemplates = false) 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<Aspect>(); var a = _world.GetAspect<Aspect>();
s.unityGameObjects.Del(oldE); a.unityGameObjects.Del(oldEntityID);
} }
_world = null; _world = null;
if (entity.TryGetID(out int newE)) if (entity.TryGetID(out int newEntityID))
{ {
_entity = entity; _entity = entity;
_world = _entity.World; _world = _entity.World;
var s = _world.GetAspect<Aspect>(); var a = _world.GetAspect<Aspect>();
if (!s.unityGameObjects.Has(newE)) s.unityGameObjects.Add(newE) = new UnityGameObject(gameObject); if (!a.unityGameObjects.Has(newEntityID))
{
a.unityGameObjects.Add(newEntityID) = new UnityGameObject(gameObject);
}
if (applyTemplates) if (applyTemplates)
{
ApplyTemplates(); ApplyTemplates();
}
} }
else else
{ {
_entity = entlong.NULL; _entity = entlong.NULL;
} }
} }
#endregion
#region ApplyTemplates
public void ApplyTemplates() public void ApplyTemplates()
{ {
ApplyTemplatesFor(_entity.ID); ApplyTemplatesFor(_entity.ID);
} }
public void ApplyTemplatesFor(int entityID) 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
} }
} }

View File

@ -1,7 +1,10 @@
#if UNITY_EDITOR #if UNITY_EDITOR
using DCFApixels.DragonECS.Unity.Internal; using DCFApixels.DragonECS.Unity.Internal;
using System.Collections.Generic;
using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using static PlasticGui.LaunchDiffParameters;
namespace DCFApixels.DragonECS.Unity.Editors namespace DCFApixels.DragonECS.Unity.Editors
{ {
@ -94,14 +97,39 @@ namespace DCFApixels.DragonECS.Unity.Editors
private void DrawTemplates() private void DrawTemplates()
{ {
EditorGUI.BeginChangeCheck();
var iterator = serializedObject.GetIterator(); var iterator = serializedObject.GetIterator();
iterator.NextVisible(true); iterator.NextVisible(true);
bool enterChildren = true; while (iterator.NextVisible(false))
while (iterator.NextVisible(enterChildren))
{ {
EditorGUILayout.PropertyField(iterator, true); 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<MonoEntityTemplate> GetTemplatesFor(Transform parent)
{
IEnumerable<MonoEntityTemplate> result = parent.GetComponents<MonoEntityTemplate>();
for (int i = 0; i < parent.childCount; i++)
{
var child = parent.GetChild(i);
if (child.TryGetComponent<EcsEntityConnect>(out _))
{
return Enumerable.Empty<MonoEntityTemplate>();
}
result = result.Concat(GetTemplatesFor(child));
}
return result;
} }
private void DrawButtons() private void DrawButtons()
@ -109,15 +137,13 @@ namespace DCFApixels.DragonECS.Unity.Editors
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
if (GUILayout.Button("Autoset")) if (GUILayout.Button("Autoset"))
{ {
Target.SetTemplates_Editor(Target.GetComponents<MonoEntityTemplate>()); Autoset(Target);
EditorUtility.SetDirty(target);
} }
if (GUILayout.Button("Autoset Cascade")) if (GUILayout.Button("Autoset Cascade"))
{ {
foreach (var item in Target.GetComponentsInChildren<EcsEntityConnect>()) foreach (var item in Target.GetComponentsInChildren<EcsEntityConnect>())
{ {
item.SetTemplates_Editor(item.GetComponents<MonoEntityTemplate>()); Autoset(item);
EditorUtility.SetDirty(item);
} }
} }
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();

View File

@ -107,43 +107,35 @@ namespace DCFApixels.DragonECS.Unity.Editors
return; return;
} }
DrawTop(target);
GUILayout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f)); GUILayout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f));
DrawTop(target);
GUILayout.Label("", GUILayout.Height(0), GUILayout.ExpandWidth(true)); GUILayout.Label("", GUILayout.Height(0), GUILayout.ExpandWidth(true));
for (int i = 0; i < componentsProp.arraySize; i++) for (int i = 0; i < componentsProp.arraySize; i++)
{ {
DrawComponentData(componentsProp.GetArrayElementAtIndex(i), i); DrawComponentData(componentsProp.GetArrayElementAtIndex(i), i);
} }
GUILayout.EndVertical(); GUILayout.EndVertical();
DrawFooter(target);
} }
private void DrawTop(ITemplateInternal target) private void DrawTop(ITemplateInternal target)
{ {
if (GUILayout.Button("Add Component", GUILayout.Height(24f))) switch (EcsGUI.Layout.AddClearComponentButtons())
{ {
Init(); case EcsGUI.AddClearComponentButton.AddComponent:
_genericMenu.ShowAsContext(); Init();
} _genericMenu.ShowAsContext();
} break;
private void DrawFooter(ITemplateInternal target) case EcsGUI.AddClearComponentButton.Clear:
{ Init();
if (GUILayout.Button("Clear", GUILayout.Height(24f))) serializedObject.FindProperty(target.ComponentsPropertyName).ClearArray();
{ serializedObject.ApplyModifiedProperties();
Init(); break;
serializedObject.FindProperty(target.ComponentsPropertyName).ClearArray();
serializedObject.ApplyModifiedProperties();
} }
} }
private void DrawComponentData(SerializedProperty componentRefProp, int index) private void DrawComponentData(SerializedProperty componentRefProp, int index)
{ {
IComponentTemplate template = componentRefProp.managedReferenceValue as IComponentTemplate; IComponentTemplate template = componentRefProp.managedReferenceValue as IComponentTemplate;
if (template == null) if (template == null || componentRefProp.managedReferenceValue == null)
{
DrawDamagedComponent(componentRefProp, index);
return;
}
if (componentRefProp.managedReferenceValue == null)
{ {
DrawDamagedComponent(componentRefProp, index); DrawDamagedComponent(componentRefProp, index);
return; return;

View File

@ -4,6 +4,7 @@ using System;
using System.Reflection; using System.Reflection;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using static UnityEngine.GraphicsBuffer;
namespace DCFApixels.DragonECS.Unity.Editors 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 RemoveButtonRect = new Rect(0f, 0f, 17f, 19f);
private static readonly Rect TooltipIconRect = new Rect(0f, 0f, 21f, 15f); 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 GUILayoutOption[] _defaultParams;
//private static bool _isInit = false; //private static bool _isInit = false;
//private static void Init() //private static void Init()
@ -27,17 +47,29 @@ namespace DCFApixels.DragonECS.Unity.Editors
// _isInit = true; // _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 public static class Layout
{ {
private static bool IsShowHidden
public static AddClearComponentButton AddClearComponentButtons()
{ {
get { return DebugMonitorPrefs.instance.IsShowHidden; } return EcsGUI.AddClearComponentButtons(GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth, 36f));
set { DebugMonitorPrefs.instance.IsShowHidden = value; }
}
private static bool IsShowRuntimeComponents
{
get { return DebugMonitorPrefs.instance.IsShowRuntimeComponents; }
set { DebugMonitorPrefs.instance.IsShowRuntimeComponents = value; }
} }
public static void DrawRuntimeComponents(entlong entity, bool isWithFoldout = true) public static void DrawRuntimeComponents(entlong entity, bool isWithFoldout = true)
{ {
@ -59,8 +91,21 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
if (isWithFoldout == false || IsShowRuntimeComponents) 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)); GUILayout.Box("", UnityEditorUtility.GetStyle(GUI.color, 0.16f), GUILayout.ExpandWidth(true));
IsShowHidden = EditorGUI.Toggle(GUILayoutUtility.GetLastRect(), "Show Hidden", IsShowHidden); IsShowHidden = EditorGUI.Toggle(GUILayoutUtility.GetLastRect(), "Show Hidden", IsShowHidden);
foreach (var componentTypeID in componentTypeIDs) foreach (var componentTypeID in componentTypeIDs)
{ {
var pool = world.GetPoolInstance(componentTypeID); var pool = world.GetPoolInstance(componentTypeID);
@ -70,13 +115,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
} }
GUILayout.EndVertical(); 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 readonly BindingFlags fieldFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
private static void DrawRuntimeComponent(int entityID, IEcsPool pool) 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) 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); bool isUnityObject = typeof(UnityEngine.Object).IsAssignableFrom(fieldType);
if (isUnityObject == false && data == null)
{
EditorGUILayout.TextField(label, "Null");
return false;
}
ref bool isExpanded = ref expandMatrix.Down(); ref bool isExpanded = ref expandMatrix.Down();
bool changed = false; bool changed = false;
outData = data; outData = data;