diff --git a/Editor.Samples/Collections/Collections_ListDrawerSettingsSample.cs b/Editor.Samples/Collections/Collections_ListDrawerSettingsSample.cs index bda8374..670adb3 100644 --- a/Editor.Samples/Collections/Collections_ListDrawerSettingsSample.cs +++ b/Editor.Samples/Collections/Collections_ListDrawerSettingsSample.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using TriInspector; using UnityEngine; @@ -12,4 +13,18 @@ public class Collections_ListDrawerSettingsSample : ScriptableObject [ListDrawerSettings(Draggable = false, AlwaysExpanded = true)] public Vector3[] vectors; + + [ListDrawerSettings(ShowElementLabels = true)] + public MyStruct[] namedStructs = new MyStruct[] + { + new MyStruct {name = "First", value = 1}, + new MyStruct {name = "Second", value = 2,}, + }; + + [Serializable] + public struct MyStruct + { + public string name; + public int value; + } } \ No newline at end of file diff --git a/Editor/Elements/TriListElement.cs b/Editor/Elements/TriListElement.cs index ac0a979..c75fce3 100644 --- a/Editor/Elements/TriListElement.cs +++ b/Editor/Elements/TriListElement.cs @@ -18,6 +18,7 @@ namespace TriInspector.Elements private readonly TriProperty _property; private readonly ReorderableList _reorderableListGui; private readonly bool _alwaysExpanded; + private readonly bool _showElementLabels; private float _lastContentWidth; @@ -29,6 +30,7 @@ namespace TriInspector.Elements _property = property; _alwaysExpanded = settings?.AlwaysExpanded ?? false; + _showElementLabels = settings?.ShowElementLabels ?? false; _reorderableListGui = new ReorderableList(null, _property.ArrayElementType) { draggable = settings?.Draggable ?? true, @@ -285,7 +287,7 @@ namespace TriInspector.Elements { return new TriPropertyElement(property, new TriPropertyElement.Props { - forceInline = true, + forceInline = !_showElementLabels, }); } @@ -345,7 +347,10 @@ namespace TriInspector.Elements rect.xMin += DraggableAreaExtraWidth; } - GetChild(index).OnGUI(rect); + using (TriPropertyOverrideContext.BeginOverride(ListPropertyOverrideContext.Instance)) + { + GetChild(index).OnGUI(rect); + } } private float ElementHeightCallback(int index) @@ -402,6 +407,28 @@ namespace TriInspector.Elements return false; } + private class ListPropertyOverrideContext : TriPropertyOverrideContext + { + public static readonly ListPropertyOverrideContext Instance = new ListPropertyOverrideContext(); + + private readonly GUIContent _noneLabel = GUIContent.none; + + public override bool TryGetDisplayName(TriProperty property, out GUIContent displayName) + { + var showLabels = property.TryGetAttribute(out ListDrawerSettingsAttribute settings) && + settings.ShowElementLabels; + + if (!showLabels) + { + displayName = _noneLabel; + return true; + } + + displayName = default; + return false; + } + } + private static class Styles { public static readonly GUIStyle ItemsCount; diff --git a/Editor/TriProperty.cs b/Editor/TriProperty.cs index ad5f64d..4c3f141 100644 --- a/Editor/TriProperty.cs +++ b/Editor/TriProperty.cs @@ -127,6 +127,10 @@ namespace TriInspector { _displayNameBackingField.text = specialName; } + else + { + _displayNameBackingField.text = TriUnityInspectorUtilities.GetStandardArrayElementName(this); + } } else { diff --git a/Editor/TriPropertyOverrideContext.cs b/Editor/TriPropertyOverrideContext.cs index 3360c0b..e63c1cd 100644 --- a/Editor/TriPropertyOverrideContext.cs +++ b/Editor/TriPropertyOverrideContext.cs @@ -28,6 +28,7 @@ namespace TriInspector { _previousContext = Current; Current = Override; + Override = null; return this; } diff --git a/Editor/Utilities/TriUnityInspectorUtilities.cs b/Editor/Utilities/TriUnityInspectorUtilities.cs index fa71a43..286e2dd 100644 --- a/Editor/Utilities/TriUnityInspectorUtilities.cs +++ b/Editor/Utilities/TriUnityInspectorUtilities.cs @@ -1,10 +1,13 @@ -using System.Reflection; +using System.Collections.Generic; +using System.Reflection; using UnityEngine; namespace TriInspector.Utilities { public class TriUnityInspectorUtilities { + private static readonly Dictionary StandardArrayElementNames = new Dictionary(); + private static readonly FieldInfo GUIStyleNameBackingField = typeof(GUIStyle) .GetField("m_Name", BindingFlags.Instance | BindingFlags.NonPublic); @@ -19,6 +22,18 @@ namespace TriInspector.Utilities return !property.IsArray && property.TryGetAttribute(out DrawWithUnityAttribute _); } + public static string GetStandardArrayElementName(TriProperty property) + { + var index = property.IndexInArray; + + if (!StandardArrayElementNames.TryGetValue(index, out var name)) + { + StandardArrayElementNames[index] = name = $"Element {index}"; + } + + return name; + } + public static bool TryGetSpecialArrayElementName(TriProperty property, out string name) { if (property.FieldType == typeof(GUIStyle) && property.Value is GUIStyle guiStyle) @@ -32,7 +47,8 @@ namespace TriInspector.Utilities property.ChildrenProperties.Count > 0 && property.ChildrenProperties[0] is var firstChild && firstChild.ValueType == typeof(string) && - firstChild.Value is string firstChildValueStr) + firstChild.Value is string firstChildValueStr && + !string.IsNullOrEmpty(firstChildValueStr)) { name = firstChildValueStr; return true; diff --git a/Runtime/Attributes/ListDrawerSettings.cs b/Runtime/Attributes/ListDrawerSettings.cs index c6de23b..fd0f368 100644 --- a/Runtime/Attributes/ListDrawerSettings.cs +++ b/Runtime/Attributes/ListDrawerSettings.cs @@ -11,5 +11,6 @@ namespace TriInspector public bool HideAddButton { get; set; } public bool HideRemoveButton { get; set; } public bool AlwaysExpanded { get; set; } + public bool ShowElementLabels { get; set; } } } \ No newline at end of file