From c5242a6e10a6d6c0697d0ef64ddbe93682b54e12 Mon Sep 17 00:00:00 2001 From: stromkuo Date: Tue, 8 Oct 2019 18:17:51 +0800 Subject: [PATCH 1/3] Make the display name customizable --- Editor/ReorderableArrayInspector.cs | 12 +++++++++++- ReorderableAttribute.cs | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Editor/ReorderableArrayInspector.cs b/Editor/ReorderableArrayInspector.cs index 73fc384..ea65fe1 100644 --- a/Editor/ReorderableArrayInspector.cs +++ b/Editor/ReorderableArrayInspector.cs @@ -162,7 +162,17 @@ public bool DoLayoutProperty(SerializedProperty property) return false; // Draw the header - string headerText = string.Format("{0} [{1}]", property.displayName, property.arraySize); + object[] attr = property.GetAttributes(); + string displayName = ""; + if (attr != null && attr.Length == 1) + { + ReorderableAttribute arrayAttr = (ReorderableAttribute)attr[0]; + if (arrayAttr != null) + { + displayName = arrayAttr.DisplayName; + } + } + string headerText = string.Format("{0} [{1}]", string.IsNullOrEmpty(displayName) ? property.displayName : displayName, property.arraySize); EditorGUILayout.PropertyField(property, new GUIContent(headerText), false); // Save header rect for handling drag and drop diff --git a/ReorderableAttribute.cs b/ReorderableAttribute.cs index f54eae1..61a542c 100644 --- a/ReorderableAttribute.cs +++ b/ReorderableAttribute.cs @@ -29,6 +29,7 @@ namespace SubjectNerd.Utilities /// public class ReorderableAttribute : PropertyAttribute { + public string DisplayName { get; protected set; } public string ElementHeader { get; protected set; } public bool HeaderZeroIndex { get; protected set; } public bool ElementSingleLine { get; protected set; } @@ -55,5 +56,13 @@ public ReorderableAttribute(string headerString = "", bool isZeroIndex = true, b HeaderZeroIndex = isZeroIndex; ElementSingleLine = isSingleLine; } + + public ReorderableAttribute(string displayName = "", string headerString = "", bool isZeroIndex = true, bool isSingleLine = false) + { + DisplayName = displayName; + ElementHeader = headerString; + HeaderZeroIndex = isZeroIndex; + ElementSingleLine = isSingleLine; + } } } From ae901e771529c40bd2ae316f2e8aaacc5b44c669 Mon Sep 17 00:00:00 2001 From: stromkuo Date: Tue, 8 Oct 2019 18:59:24 +0800 Subject: [PATCH 2/3] Fix indentation --- Editor/ReorderableArrayInspector.cs | 20 ++++++++++---------- ReorderableAttribute.cs | 12 ++++++------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Editor/ReorderableArrayInspector.cs b/Editor/ReorderableArrayInspector.cs index ea65fe1..0dfd904 100644 --- a/Editor/ReorderableArrayInspector.cs +++ b/Editor/ReorderableArrayInspector.cs @@ -163,16 +163,16 @@ public bool DoLayoutProperty(SerializedProperty property) // Draw the header object[] attr = property.GetAttributes(); - string displayName = ""; - if (attr != null && attr.Length == 1) - { - ReorderableAttribute arrayAttr = (ReorderableAttribute)attr[0]; - if (arrayAttr != null) - { - displayName = arrayAttr.DisplayName; - } - } - string headerText = string.Format("{0} [{1}]", string.IsNullOrEmpty(displayName) ? property.displayName : displayName, property.arraySize); + string displayName = ""; + if (attr != null && attr.Length == 1) + { + ReorderableAttribute arrayAttr = (ReorderableAttribute)attr[0]; + if (arrayAttr != null) + { + displayName = arrayAttr.DisplayName; + } + } + string headerText = string.Format("{0} [{1}]", string.IsNullOrEmpty(displayName) ? property.displayName : displayName, property.arraySize); EditorGUILayout.PropertyField(property, new GUIContent(headerText), false); // Save header rect for handling drag and drop diff --git a/ReorderableAttribute.cs b/ReorderableAttribute.cs index 61a542c..016f56f 100644 --- a/ReorderableAttribute.cs +++ b/ReorderableAttribute.cs @@ -58,11 +58,11 @@ public ReorderableAttribute(string headerString = "", bool isZeroIndex = true, b } public ReorderableAttribute(string displayName = "", string headerString = "", bool isZeroIndex = true, bool isSingleLine = false) - { - DisplayName = displayName; - ElementHeader = headerString; - HeaderZeroIndex = isZeroIndex; - ElementSingleLine = isSingleLine; - } + { + DisplayName = displayName; + ElementHeader = headerString; + HeaderZeroIndex = isZeroIndex; + ElementSingleLine = isSingleLine; + } } } From a36b983005dd8316f639568ebe064322baaeedf6 Mon Sep 17 00:00:00 2001 From: stromkuo Date: Wed, 9 Oct 2019 17:38:28 +0800 Subject: [PATCH 3/3] Make GetAttributes work in inheritance --- Editor/SerializedPropExtension.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Editor/SerializedPropExtension.cs b/Editor/SerializedPropExtension.cs index 67a36ab..2e0c6aa 100644 --- a/Editor/SerializedPropExtension.cs +++ b/Editor/SerializedPropExtension.cs @@ -267,7 +267,16 @@ public static object[] GetAttributes(this SerializedProperty prop) | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public; - FieldInfo field = objType.GetField(prop.name, bindingFlags); + FieldInfo field = null; + while (objType != null) + { + field = objType.GetField(prop.name, bindingFlags); + if (field != null) + { + break; + } + objType = objType.BaseType; + } if (field != null) return field.GetCustomAttributes(attrType, true); return new object[0];