update runtime components inspector

This commit is contained in:
DCFApixels 2025-05-10 11:45:24 +08:00
parent 760c789acb
commit 14163956e0
5 changed files with 270 additions and 41 deletions

View File

@ -143,10 +143,10 @@ namespace DCFApixels.DragonECS.Unity.Editors
private readonly int _value; private readonly int _value;
public IndentLevelScope(int value) public IndentLevelScope(int value)
{ {
_value = EditorGUI.indentLevel; _value = IndentLevel;
EditorGUI.indentLevel = value; IndentLevel = value;
} }
public void Dispose() { EditorGUI.indentLevel = _value; } public void Dispose() { IndentLevel = _value; }
} }
public struct AlignmentScope : IDisposable public struct AlignmentScope : IDisposable
{ {
@ -261,7 +261,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
public static AlignmentScope SetAlignment(TextAnchor value) => new AlignmentScope(GUI.skin.label, value); public static AlignmentScope SetAlignment(TextAnchor value) => new AlignmentScope(GUI.skin.label, value);
public static AlignmentScope SetAlignment(GUIStyle target) => new AlignmentScope(target); public static AlignmentScope SetAlignment(GUIStyle target) => new AlignmentScope(target);
public static IndentLevelScope SetIndentLevel(int level) => new IndentLevelScope(level); public static IndentLevelScope SetIndentLevel(int level) => new IndentLevelScope(level);
public static IndentLevelScope UpIndentLevel() => new IndentLevelScope(EditorGUI.indentLevel + 1); public static IndentLevelScope UpIndentLevel() => new IndentLevelScope(IndentLevel + 1);
public static ContentColorScope SetContentColor(Color value) => new ContentColorScope(value); public static ContentColorScope SetContentColor(Color value) => new ContentColorScope(value);
public static ContentColorScope SetContentColor(Color value, float a) => new ContentColorScope(value.r, value.g, value.b, a); public static ContentColorScope SetContentColor(Color value, float a) => new ContentColorScope(value.r, value.g, value.b, a);
public static ContentColorScope SetContentColor(float r, float g, float b, float a = 1f) => new ContentColorScope(r, g, b, a); public static ContentColorScope SetContentColor(float r, float g, float b, float a = 1f) => new ContentColorScope(r, g, b, a);
@ -286,12 +286,28 @@ namespace DCFApixels.DragonECS.Unity.Editors
internal static readonly Rect HeadIconsRect = new Rect(0f, 0f, 19f, 19f); internal static readonly Rect HeadIconsRect = new Rect(0f, 0f, 19f, 19f);
public static float EntityBarHeight => EditorGUIUtility.singleLineHeight + 3f;
private static float indent => (float)EditorGUI.indentLevel * 15f;
private static float indentLevel => EditorGUI.indentLevel;
#region Properties #region Properties
public static float EntityBarHeight
{
get => EditorGUIUtility.singleLineHeight + 3f;
}
public static float Indent
{
get => EditorGUI.indentLevel * 15f;
}
public static int IndentLevel
{
get => EditorGUI.indentLevel;
set => EditorGUI.indentLevel = value;
}
public static float OneLineHeight
{
get => EditorGUIUtility.singleLineHeight;
}
public static float Spacing
{
get => EditorGUIUtility.standardVerticalSpacing;
}
private static ComponentColorMode AutoColorMode private static ComponentColorMode AutoColorMode
{ {
get { return UserSettingsPrefs.instance.ComponentColorMode; } get { return UserSettingsPrefs.instance.ComponentColorMode; }
@ -312,14 +328,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
// get { return UserSettingsPrefs.instance.IsFastModeRuntimeComponents; } // get { return UserSettingsPrefs.instance.IsFastModeRuntimeComponents; }
// set { UserSettingsPrefs.instance.IsFastModeRuntimeComponents = value; } // set { UserSettingsPrefs.instance.IsFastModeRuntimeComponents = value; }
//} //}
private static float OneLineHeight
{
get => EditorGUIUtility.singleLineHeight;
}
private static float Spacing
{
get => EditorGUIUtility.standardVerticalSpacing;
}
#endregion #endregion
#region enums #region enums

View File

@ -69,6 +69,26 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
public readonly bool IsCompositeType; public readonly bool IsCompositeType;
public readonly bool IsUnmanaged; public readonly bool IsUnmanaged;
public readonly bool IsLeaf;
public readonly LeafType LeafPropertyType;
public enum LeafType
{
NONE = 0,
Enum,
Bool,
String,
Float,
Double,
Byte,
SByte,
Short,
UShort,
Int,
UInt,
Long,
ULong,
}
public readonly FieldInfoData[] Fields; public readonly FieldInfoData[] Fields;
private RefEditorWrapper[] _wrappers = new RefEditorWrapper[2]; private RefEditorWrapper[] _wrappers = new RefEditorWrapper[2];
@ -79,11 +99,71 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
public RuntimeComponentReflectionCache(Type type) public RuntimeComponentReflectionCache(Type type)
{ {
Type = type; Type = type;
ResetWrappers(); ResetWrappers();
IsUnmanaged = UnsafeUtility.IsUnmanaged(type); IsUnmanaged = UnsafeUtility.IsUnmanaged(type);
IsUnityObjectType = typeof(UnityObject).IsAssignableFrom(type); IsUnityObjectType = typeof(UnityObject).IsAssignableFrom(type);
LeafPropertyType = LeafType.NONE;
IsLeaf = type.IsPrimitive || type == typeof(string) || type.IsEnum;
if (IsLeaf)
{
if (type.IsEnum)
{
LeafPropertyType = LeafType.Enum;
}
else if (type == typeof(bool))
{
LeafPropertyType = LeafType.Bool;
}
else if (type == typeof(string))
{
LeafPropertyType = LeafType.String;
}
else if (type == typeof(float))
{
LeafPropertyType = LeafType.Float;
}
else if (type == typeof(double))
{
LeafPropertyType = LeafType.Double;
}
else if (type == typeof(byte))
{
LeafPropertyType = LeafType.Byte;
}
else if (type == typeof(sbyte))
{
LeafPropertyType = LeafType.SByte;
}
else if (type == typeof(short))
{
LeafPropertyType = LeafType.Short;
}
else if (type == typeof(ushort))
{
LeafPropertyType = LeafType.UShort;
}
else if (type == typeof(int))
{
LeafPropertyType = LeafType.Int;
}
else if (type == typeof(uint))
{
LeafPropertyType = LeafType.UInt;
}
else if (type == typeof(long))
{
LeafPropertyType = LeafType.Long;
}
else if (type == typeof(ulong))
{
LeafPropertyType = LeafType.ULong;
}
}
IsUnitySerializable = IsUnitySerializable =
IsUnityObjectType || IsUnityObjectType ||
//typeof(Array).IsAssignableFrom(type) || //typeof(Array).IsAssignableFrom(type) ||
@ -232,7 +312,7 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
} }
RuntimeComponentReflectionCache.FieldInfoData componentInfoData = new RuntimeComponentReflectionCache.FieldInfoData(null, componentType, meta.Name); RuntimeComponentReflectionCache.FieldInfoData componentInfoData = new RuntimeComponentReflectionCache.FieldInfoData(null, componentType, meta.Name);
if (DrawRuntimeData(ref componentInfoData, UnityEditorUtility.GetLabel(meta.Name), expandMatrix, data, out object resultData)) if (DrawRuntimeData(ref componentInfoData, UnityEditorUtility.GetLabel(meta.Name), expandMatrix, data, out object resultData, 0))
{ {
cmp.SetRaw(worldID, resultData); cmp.SetRaw(worldID, resultData);
} }
@ -354,7 +434,7 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
RuntimeComponentReflectionCache.FieldInfoData componentInfoData = new RuntimeComponentReflectionCache.FieldInfoData(null, componentType, meta.Name); RuntimeComponentReflectionCache.FieldInfoData componentInfoData = new RuntimeComponentReflectionCache.FieldInfoData(null, componentType, meta.Name);
if (DrawRuntimeData(ref componentInfoData, UnityEditorUtility.GetLabel(meta.Name), expandMatrix, data, out object resultData)) if (DrawRuntimeData(ref componentInfoData, UnityEditorUtility.GetLabel(meta.Name), expandMatrix, data, out object resultData, 0))
{ {
pool.SetRaw(entityID, resultData); pool.SetRaw(entityID, resultData);
} }
@ -366,8 +446,9 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
#region draw data #region draw data
private bool DrawRuntimeData(ref RuntimeComponentReflectionCache.FieldInfoData fieldInfoData, GUIContent label, ExpandMatrix expandMatrix, object data, out object outData) private bool DrawRuntimeData(ref RuntimeComponentReflectionCache.FieldInfoData fieldInfoData, GUIContent label, ExpandMatrix expandMatrix, object data, out object outData, int depth)
{ {
const int DEPTH_MAX = 64;
outData = data; outData = data;
Type type = data == null ? typeof(void) : data.GetType(); Type type = data == null ? typeof(void) : data.GetType();
@ -379,14 +460,19 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
EditorGUILayout.TextField(label, "Null"); EditorGUILayout.TextField(label, "Null");
return false; return false;
} }
if (depth >= DEPTH_MAX || cache == null)
{
EditorGUILayout.TextField(label, "error");
return false;
}
bool isUnityObjectType = cache.IsUnityObjectType; bool isUnityObjectType = cache.IsUnityObjectType;
ref bool isExpanded = ref expandMatrix.Down(); ref bool isExpanded = ref expandMatrix.Down();
bool changed = false; bool changed = false;
if (cache.IsUnitySerializable == false && cache.IsCompositeType) if (cache.IsUnitySerializable == false && cache.IsCompositeType)
{ {
GUILayout.Space(EcsGUI.Spacing);
var foldoutStyle = EditorStyles.foldout; var foldoutStyle = EditorStyles.foldout;
Rect rect = GUILayoutUtility.GetRect(label, foldoutStyle); Rect rect = GUILayoutUtility.GetRect(label, foldoutStyle);
rect.xMin += EcsGUI.Indent; rect.xMin += EcsGUI.Indent;
@ -396,13 +482,11 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
if (isExpanded) if (isExpanded)
{ {
using (EcsGUI.UpIndentLevel()) using (EcsGUI.UpIndentLevel())
{
if (cache != null)
{ {
for (int j = 0, jMax = cache.Fields.Length; j < jMax; j++) for (int j = 0, jMax = cache.Fields.Length; j < jMax; j++)
{ {
var field = cache.Fields[j]; var field = cache.Fields[j];
if (DrawRuntimeData(ref field, UnityEditorUtility.GetLabel(field.UnityFormatName), expandMatrix, field.FieldInfo.GetValue(data), out object fieldData)) if (DrawRuntimeData(ref field, UnityEditorUtility.GetLabel(field.UnityFormatName), expandMatrix, field.FieldInfo.GetValue(data), out object fieldData, depth + 1))
{ {
field.FieldInfo.SetValue(data, fieldData); field.FieldInfo.SetValue(data, fieldData);
outData = data; outData = data;
@ -410,7 +494,6 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
} }
} }
} }
}
} }
} }
@ -451,15 +534,154 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
try try
{ {
if (cache.IsCompositeType && fieldInfoData.IsPassToUnitySerialize) if (fieldInfoData.IsPassToUnitySerialize)
{
if (cache.IsCompositeType)
{ {
wrapper.SO.Update(); wrapper.SO.Update();
wrapper.IsExpanded = isExpanded; wrapper.IsExpanded = isExpanded;
EditorGUILayout.PropertyField(wrapper.Property, label, true); EditorGUILayout.PropertyField(wrapper.Property, label, true);
if (GUI.changed)
{
wrapper.SO.ApplyModifiedProperties();
}
}
else if (cache.IsLeaf)
{
var eventType = Event.current.type;
switch (cache.LeafPropertyType)
{
//case RuntimeComponentReflectionCache.LeafType.Enum:
// break;
case RuntimeComponentReflectionCache.LeafType.Bool:
if (eventType != EventType.Layout)
{
wrapper.data = EditorGUILayout.Toggle(label, (bool)data);
}
else
{
EditorGUILayout.Toggle(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.String:
if (eventType != EventType.Layout)
{
wrapper.data = EditorGUILayout.TextField(label, (string)data);
}
else
{
EditorGUILayout.TextField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.Float:
if (eventType != EventType.Layout)
{
wrapper.data = EditorGUILayout.FloatField(label, (float)data);
}
else
{
EditorGUILayout.FloatField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.Double:
if (eventType != EventType.Layout)
{
wrapper.data = EditorGUILayout.DoubleField(label, (double)data);
}
else
{
EditorGUILayout.DoubleField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.Byte:
if (eventType != EventType.Layout)
{
wrapper.data = (byte)EditorGUILayout.IntField(label, (byte)data);
}
else
{
EditorGUILayout.IntField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.SByte:
if (eventType != EventType.Layout)
{
wrapper.data = (sbyte)EditorGUILayout.IntField(label, (sbyte)data);
}
else
{
EditorGUILayout.IntField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.Short:
if (eventType != EventType.Layout)
{
wrapper.data = (short)EditorGUILayout.IntField(label, (short)data);
}
else
{
EditorGUILayout.IntField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.UShort:
if (eventType != EventType.Layout)
{
wrapper.data = (ushort)EditorGUILayout.IntField(label, (ushort)data);
}
else
{
EditorGUILayout.IntField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.Int:
if (eventType != EventType.Layout)
{
wrapper.data = (int)EditorGUILayout.IntField(label, (int)data);
}
else
{
EditorGUILayout.IntField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.UInt:
if (eventType != EventType.Layout)
{
wrapper.data = (uint)EditorGUILayout.IntField(label, (int)(uint)data);
}
else
{
EditorGUILayout.IntField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.Long:
if (eventType != EventType.Layout)
{
wrapper.data = EditorGUILayout.LongField(label, (long)data);
}
else
{
EditorGUILayout.LongField(label, default);
}
break;
case RuntimeComponentReflectionCache.LeafType.ULong:
if (eventType != EventType.Layout)
{
wrapper.data = (ulong)EditorGUILayout.LongField(label, (long)(ulong)data);
}
else
{
EditorGUILayout.LongField(label, default);
}
break;
default:
EditorGUILayout.LabelField(label);
break;
}
}
} }
else else
{ {
//EcsGUI.
EditorGUILayout.LabelField(label); EditorGUILayout.LabelField(label);
} }
} }
@ -474,7 +696,6 @@ namespace DCFApixels.DragonECS.Unity.Editors.X
{ {
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())
{ {
wrapper.SO.ApplyModifiedProperties();
outData = wrapper.Data; outData = wrapper.Data;
changed = true; changed = true;
} }

View File

@ -21,7 +21,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
} }
using (EcsGUI.SetIndentLevel(IsArrayElement ? EditorGUI.indentLevel : EditorGUI.indentLevel + 1)) using (EcsGUI.SetIndentLevel(IsArrayElement ? EcsGUI.IndentLevel : EcsGUI.IndentLevel + 1))
{ {
Rect subPosition = position; Rect subPosition = position;
int depth = -1; int depth = -1;

View File

@ -363,7 +363,7 @@ namespace DCFApixels.DragonECS.Unity.Docs.Editors
if (_searchingSample.Length == 0) if (_searchingSample.Length == 0)
{ {
EditorGUI.indentLevel = groupInfo.Depth; EcsGUI.IndentLevel = groupInfo.Depth;
} }
GUIContent label = UnityEditorUtility.GetLabel(_searchingSample.Length == 0 ? groupInfo.Name : groupInfo.Path); GUIContent label = UnityEditorUtility.GetLabel(_searchingSample.Length == 0 ? groupInfo.Name : groupInfo.Path);