Skip to content

Commit f6789d2

Browse files
authored
Merge pull request #181 from brunomikoski/feature/omit-labels-in-arrays-and-lists
Feature/omit labels in arrays and lists
2 parents e46174e + 43dac3e commit f6789d2

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Scripts/Editor/Extensions/SerializedPropertyExtensions.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Text.RegularExpressions;
67
using UnityEditor;
78
using UnityEngine;
89
using Object = UnityEngine.Object;
@@ -301,5 +302,49 @@ static T DescendHierarchy<T>(object targetObject, List<string> splitName, List<i
301302

302303
return DescendHierarchy<T>(newObj, splitName, splitCounts, depth + 1);
303304
}
305+
306+
/// <summary>
307+
/// From: https://gist.github.com/monry/9de7009689cbc5050c652bcaaaa11daa
308+
/// </summary>
309+
public static SerializedProperty GetParent(this SerializedProperty serializedProperty)
310+
{
311+
string[] propertyPaths = serializedProperty.propertyPath.Split('.');
312+
if (propertyPaths.Length <= 1)
313+
return default;
314+
315+
SerializedProperty parentSerializedProperty =
316+
serializedProperty.serializedObject.FindProperty(propertyPaths.First());
317+
for (int index = 1; index < propertyPaths.Length - 1; index++)
318+
{
319+
if (propertyPaths[index] == "Array")
320+
{
321+
// Reached the end
322+
if (index + 1 == propertyPaths.Length - 1)
323+
break;
324+
325+
if (propertyPaths.Length > index + 1 && Regex.IsMatch(propertyPaths[index + 1], "^data\\[\\d+\\]$"))
326+
{
327+
Match match = Regex.Match(propertyPaths[index + 1], "^data\\[(\\d+)\\]$");
328+
int arrayIndex = int.Parse(match.Groups[1].Value);
329+
parentSerializedProperty = parentSerializedProperty.GetArrayElementAtIndex(arrayIndex);
330+
index++;
331+
}
332+
333+
continue;
334+
}
335+
336+
parentSerializedProperty = parentSerializedProperty.FindPropertyRelative(propertyPaths[index]);
337+
}
338+
339+
return parentSerializedProperty;
340+
}
341+
342+
public static bool IsInArray(this SerializedProperty serializedProperty)
343+
{
344+
SerializedProperty parent = serializedProperty.GetParent();
345+
if (parent == null)
346+
return false;
347+
return parent.isArray;
348+
}
304349
}
305350
}

Scripts/Editor/PropertyDrawers/CollectionItemPropertyDrawer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
7070
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
7171
{
7272
Initialize(property);
73+
74+
// If this property is inside of a list, don't show a label. It would just say something like "Element 0"
75+
// anyway which is not useful. Might as well have a bit more room to see what the value says.
76+
if (property.IsInArray())
77+
label = GUIContent.none;
7378

7479
if (OptionsAttribute.DrawType == DrawType.AsReference)
7580
{
@@ -342,4 +347,4 @@ public void OverrideFieldInfo(FieldInfo targetFieldInfo)
342347
overrideFieldInfo = targetFieldInfo;
343348
}
344349
}
345-
}
350+
}

0 commit comments

Comments
 (0)