This commit is contained in:
Mikhail 2023-05-27 09:14:36 +08:00
parent 8cfc44e7c6
commit 21a1b40a1e
5 changed files with 60 additions and 10 deletions

View File

@ -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<GUIStyle>();
}
private static SparseArray<GUIStyle> colorBoxeStyles = new SparseArray<GUIStyle>();
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)

View File

@ -32,5 +32,9 @@ namespace DCFApixels.DragonECS
g.OnGizmos(transform, ITemplateComponentGizmos.Mode.Selected);
}
}
public void Clear()
{
_components = new ITemplateComponent[0];
}
}
}

View File

@ -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<DebugDescriptionAttribute>()?.description;
Color panelColor = customInitializer != null ? customInitializer.Color : componentType.GetCustomAttribute<DebugColorAttribute>()?.GetUnityColor() ?? Color.black;
string description = customInitializer != null ? customInitializer.Description : initializerType.GetCustomAttribute<DebugDescriptionAttribute>()?.description;
Color panelColor = customInitializer != null ? customInitializer.Color : initializerType.GetCustomAttribute<DebugColorAttribute>()?.GetUnityColor() ?? Color.black;
GUILayout.BeginHorizontal();
@ -160,7 +177,14 @@ namespace DCFApixels.DragonECS
EditorGUI.BeginChangeCheck();
GUIContent label = new GUIContent(name, $"{name} ");
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();

View File

@ -20,5 +20,10 @@ namespace DCFApixels.DragonECS
foreach (var item in _components)
item.Add(world, entityID);
}
public void Clear()
{
_components = new ITemplateComponent[0];
}
}
}

View File

@ -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);