Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the display name customizable #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Editor/ReorderableArrayInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReorderableAttribute>();
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
Expand Down
11 changes: 10 additions & 1 deletion Editor/SerializedPropExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,16 @@ public static object[] GetAttributes<T>(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];
Expand Down
9 changes: 9 additions & 0 deletions ReorderableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace SubjectNerd.Utilities
/// </summary>
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; }
Expand All @@ -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;
}
}
}