From 21a1b40a1eceb776b9c4189a2c348339f92e0469 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sat, 27 May 2023 09:14:36 +0800 Subject: [PATCH] fixes --- src/Debug/Editor/EcsEditor.cs | 14 +++++++- src/EntityTemplate/EntityTemplate.cs | 4 +++ src/EntityTemplate/EntityTemplateEditor.cs | 42 +++++++++++++++++----- src/EntityTemplate/EntityTemplatePreset.cs | 5 +++ src/EntityTemplate/TemplateComponent.cs | 5 +++ 5 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/Debug/Editor/EcsEditor.cs b/src/Debug/Editor/EcsEditor.cs index 1597de2..3bbd300 100644 --- a/src/Debug/Editor/EcsEditor.cs +++ b/src/Debug/Editor/EcsEditor.cs @@ -1,12 +1,18 @@ #if UNITY_EDITOR using System; using System.Runtime.InteropServices; +using UnityEditor; using UnityEngine; namespace DCFApixels.DragonECS.Editors { + [InitializeOnLoad] public static class EcsEditor { + static EcsEditor() + { + colorBoxeStyles = new SparseArray(); + } private static SparseArray colorBoxeStyles = new SparseArray(); public static GUIStyle GetStyle(Color color, float alphaMultiplier) { @@ -18,8 +24,11 @@ namespace DCFApixels.DragonECS.Editors int colorCode = new Color32Union(color32).colorCode; if (colorBoxeStyles.TryGetValue(colorCode, out GUIStyle style)) { - if (style == null) + if (style == null || style.normal.background == null) + { style = CreateStyle(color32, colorCode); + colorBoxeStyles[colorCode] = style; + } return style; } @@ -32,6 +41,9 @@ namespace DCFApixels.DragonECS.Editors GUIStyle result = new GUIStyle(GUI.skin.box); Color componentColor = color32; result.normal.background = CreateTexture(2, 2, componentColor); + result.active.background = CreateTexture(2, 2, componentColor); + result.hover.background = CreateTexture(2, 2, componentColor); + result.focused.background = CreateTexture(2, 2, componentColor); return result; } private static Texture2D CreateTexture(int width, int height, Color color) diff --git a/src/EntityTemplate/EntityTemplate.cs b/src/EntityTemplate/EntityTemplate.cs index 62a5ed4..837cae8 100644 --- a/src/EntityTemplate/EntityTemplate.cs +++ b/src/EntityTemplate/EntityTemplate.cs @@ -32,5 +32,9 @@ namespace DCFApixels.DragonECS g.OnGizmos(transform, ITemplateComponentGizmos.Mode.Selected); } } + public void Clear() + { + _components = new ITemplateComponent[0]; + } } } diff --git a/src/EntityTemplate/EntityTemplateEditor.cs b/src/EntityTemplate/EntityTemplateEditor.cs index cb7054d..78f66c3 100644 --- a/src/EntityTemplate/EntityTemplateEditor.cs +++ b/src/EntityTemplate/EntityTemplateEditor.cs @@ -9,7 +9,7 @@ namespace DCFApixels.DragonECS using UnityEditor; using UnityEngine; - public class EntityTemplateEditorBase: Editor + public abstract class EntityTemplateEditorBase: Editor { private static readonly Rect RemoveButtonRect = new Rect(0f, 0f, 15f, 15f); private static readonly Rect TooltipIconRect = new Rect(0f, 0f, 15f, 15f); @@ -115,6 +115,7 @@ namespace DCFApixels.DragonECS DrawComponentData(componentsProp.GetArrayElementAtIndex(i), i); GUILayout.Space(EditorGUIUtility.standardVerticalSpacing * 2); } + DrawFooter(target); } private void DrawTop(ITemplateInternal target) { @@ -124,9 +125,23 @@ namespace DCFApixels.DragonECS genericMenu.ShowAsContext(); } } + private void DrawFooter(ITemplateInternal target) + { + if (GUILayout.Button("Clear", GUILayout.Height(24f))) + { + Init(); + serializedObject.FindProperty(target.ComponentsPropertyName).ClearArray(); + serializedObject.ApplyModifiedProperties(); + } + } private void DrawComponentData(SerializedProperty componentRefProp, int index) { - ITemplateComponent browsable = (ITemplateComponent)componentRefProp.managedReferenceValue; + ITemplateComponent browsable = componentRefProp.managedReferenceValue as ITemplateComponent; + if(browsable == null) + { + DrawDamagedComponent(componentRefProp, index); + return; + } ITemplateComponentName browsableName = browsable as ITemplateComponentName; if (componentRefProp.managedReferenceValue == null) @@ -135,24 +150,26 @@ namespace DCFApixels.DragonECS return; } + Type initializerType; Type componentType; SerializedProperty componentProperty = componentRefProp; TemplateComponentInitializerBase customInitializer = componentProperty.managedReferenceValue as TemplateComponentInitializerBase; if (customInitializer != null) { - componentProperty = componentProperty.FindPropertyRelative("component"); - componentType = customInitializer.Type; + componentProperty = componentRefProp.FindPropertyRelative("component"); + initializerType = customInitializer.Type; + componentType = customInitializer.GetType().GetField("component", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FieldType; } else { - componentType = componentProperty.managedReferenceValue.GetType(); + initializerType = componentProperty.managedReferenceValue.GetType(); + componentType = initializerType; } Type type = browsable.GetType(); string name = browsableName == null ? type.Name : GetLastPathComponent(browsableName.Name); - string description = customInitializer != null ? customInitializer.Description : componentType.GetCustomAttribute()?.description; - Color panelColor = customInitializer != null ? customInitializer.Color : componentType.GetCustomAttribute()?.GetUnityColor() ?? Color.black; - + string description = customInitializer != null ? customInitializer.Description : initializerType.GetCustomAttribute()?.description; + Color panelColor = customInitializer != null ? customInitializer.Color : initializerType.GetCustomAttribute()?.GetUnityColor() ?? Color.black; GUILayout.BeginHorizontal(); @@ -160,7 +177,14 @@ namespace DCFApixels.DragonECS EditorGUI.BeginChangeCheck(); GUIContent label = new GUIContent(name, $"{name} "); - EditorGUILayout.PropertyField(componentProperty, label, true); + if (componentType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Length <= 0) + { + GUILayout.Label(label); + } + else + { + EditorGUILayout.PropertyField(componentProperty, label, true); + } if (EditorGUI.EndChangeCheck()) { componentProperty.serializedObject.ApplyModifiedProperties(); diff --git a/src/EntityTemplate/EntityTemplatePreset.cs b/src/EntityTemplate/EntityTemplatePreset.cs index 7f81632..2de34e3 100644 --- a/src/EntityTemplate/EntityTemplatePreset.cs +++ b/src/EntityTemplate/EntityTemplatePreset.cs @@ -20,5 +20,10 @@ namespace DCFApixels.DragonECS foreach (var item in _components) item.Add(world, entityID); } + + public void Clear() + { + _components = new ITemplateComponent[0]; + } } } diff --git a/src/EntityTemplate/TemplateComponent.cs b/src/EntityTemplate/TemplateComponent.cs index 1cbe3b4..a2486c7 100644 --- a/src/EntityTemplate/TemplateComponent.cs +++ b/src/EntityTemplate/TemplateComponent.cs @@ -33,6 +33,8 @@ namespace DCFApixels.DragonECS public virtual string Description => string.Empty; public abstract Type Type { get; } + internal abstract object ComponentRef { get; } + #region Get meta internal static Color GetColor(Type type) { @@ -85,6 +87,9 @@ namespace DCFApixels.DragonECS public override Color Color => _autoColor; public override string Description => _autoDescription; public sealed override Type Type => typeof(T); + + internal T Component => component; + internal override object ComponentRef => component; #endregion public abstract void Add(EcsWorld w, int e);