Skip to content

Commit b5bc7ea

Browse files
committed
Handle LabelByChild functionality directly in the property handler
1 parent ee37110 commit b5bc7ea

5 files changed

Lines changed: 92 additions & 2 deletions

File tree

Assets/Editor Toolbox/Editor/Drawers/Regular/LabelByChildAttributeDrawer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using UnityEditor;
1+
using System;
2+
3+
using UnityEditor;
24
using UnityEngine;
35

46
namespace Toolbox.Editor.Drawers
57
{
6-
[CustomPropertyDrawer(typeof(LabelByChildAttribute))]
8+
[Obsolete("For now the LabelByChildAttribute is handled internally by the ToolboxPropertyHandler.")]
79
public class LabelByChildAttributeDrawer : PropertyDrawerBase
810
{
911
private GUIContent GetLabelByValue(SerializedProperty property, GUIContent label)

Assets/Editor Toolbox/Editor/ToolboxPropertyHandler.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ private void HandleNewAttribute(PropertyAttribute attribute)
177177
return;
178178
case NewLabelAttribute a:
179179
label.text = a.NewLabel;
180+
return;
181+
case LabelByChildAttribute a:
182+
if (!isArray)
183+
{
184+
PropertyUtility.OverrideLabelByChild(label, property, a.ChildName);
185+
}
186+
180187
return;
181188
}
182189
}

Assets/Editor Toolbox/Editor/Utilities/PropertyUtility.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55

66
using UnityEditor;
7+
using UnityEngine;
78
using Object = UnityEngine.Object;
89

910
namespace Toolbox.Editor
@@ -335,6 +336,86 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
335336
return field;
336337
}
337338

339+
public static void OverrideLabelByChild(GUIContent label, SerializedProperty property, string childName)
340+
{
341+
var childProperty = property.FindPropertyRelative(childName);
342+
if (childProperty != null)
343+
{
344+
OverrideLabelByValue(label, childProperty);
345+
}
346+
else
347+
{
348+
label.text = "<cannot find child property>";
349+
}
350+
}
351+
352+
public static void OverrideLabelByValue(GUIContent label, SerializedProperty property)
353+
{
354+
switch (property.propertyType)
355+
{
356+
case SerializedPropertyType.Generic:
357+
break;
358+
case SerializedPropertyType.Integer:
359+
label.text = property.intValue.ToString();
360+
break;
361+
case SerializedPropertyType.Boolean:
362+
label.text = property.boolValue.ToString();
363+
break;
364+
case SerializedPropertyType.Float:
365+
label.text = property.floatValue.ToString();
366+
break;
367+
case SerializedPropertyType.String:
368+
label.text = property.stringValue;
369+
break;
370+
case SerializedPropertyType.Color:
371+
label.text = property.colorValue.ToString();
372+
break;
373+
case SerializedPropertyType.ObjectReference:
374+
label.text = property.objectReferenceValue ? property.objectReferenceValue.name : "null";
375+
break;
376+
case SerializedPropertyType.LayerMask:
377+
switch (property.intValue)
378+
{
379+
case 0:
380+
label.text = "Nothing";
381+
break;
382+
case ~0:
383+
label.text = "Everything";
384+
break;
385+
default:
386+
label.text = LayerMask.LayerToName((int)Mathf.Log(property.intValue, 2));
387+
break;
388+
}
389+
break;
390+
case SerializedPropertyType.Enum:
391+
label.text = property.enumNames[property.enumValueIndex];
392+
break;
393+
case SerializedPropertyType.Vector2:
394+
label.text = property.vector2Value.ToString();
395+
break;
396+
case SerializedPropertyType.Vector3:
397+
label.text = property.vector3Value.ToString();
398+
break;
399+
case SerializedPropertyType.Vector4:
400+
label.text = property.vector4Value.ToString();
401+
break;
402+
case SerializedPropertyType.Rect:
403+
label.text = property.rectValue.ToString();
404+
break;
405+
case SerializedPropertyType.Character:
406+
label.text = ((char)property.intValue).ToString();
407+
break;
408+
case SerializedPropertyType.Bounds:
409+
label.text = property.boundsValue.ToString();
410+
break;
411+
case SerializedPropertyType.Quaternion:
412+
label.text = property.quaternionValue.ToString();
413+
break;
414+
default:
415+
break;
416+
}
417+
}
418+
338419

339420
internal static class Defaults
340421
{

Assets/Editor Toolbox/Runtime/Attributes/Regular/LabelByChildAttribute.cs renamed to Assets/Editor Toolbox/Runtime/Attributes/Special/LabelByChildAttribute.cs

File renamed without changes.

Assets/Editor Toolbox/Runtime/Attributes/Regular/LabelByChildAttribute.cs.meta renamed to Assets/Editor Toolbox/Runtime/Attributes/Special/LabelByChildAttribute.cs.meta

File renamed without changes.

0 commit comments

Comments
 (0)