Skip to content

Commit 3cd2793

Browse files
author
jhodk
committed
fix: set label text before BeginProperty call
1 parent 5fadcb6 commit 3cd2793

3 files changed

Lines changed: 41 additions & 9 deletions

File tree

Assets/Example/Example.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public abstract class Food
99
public string name;
1010

1111
public float kcal;
12+
13+
public override string ToString () => $"{name} ({kcal} kcal)";
1214
}
1315

1416
[Serializable]
@@ -89,6 +91,15 @@ public class Example : MonoBehaviour
8991
new Peach(),
9092
new Grape()
9193
};
94+
95+
// UseToStringAsLabel support on UNITY_2021_3_OR_NEWER
96+
[SerializeReference, SubclassSelector(UseToStringAsLabel = true)]
97+
public List<Food> foodsThree = new List<Food>
98+
{
99+
new Apple(),
100+
new Peach(),
101+
new Grape()
102+
};
92103
}
93104

94105
#if UNITY_EDITOR

Assets/Example/Example.unity

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,28 @@ MonoBehaviour:
260260
- rid: 9020349853700980772
261261
- rid: 9020349853700980773
262262
- rid: 9020349853700980774
263+
foodsThree:
264+
- rid: 7418200600248058228
265+
- rid: 7418200600248058229
266+
- rid: 7418200600248058230
263267
references:
264268
version: 2
265269
RefIds:
270+
- rid: 7418200600248058228
271+
type: {class: Apple, ns: , asm: Assembly-CSharp}
272+
data:
273+
name: Apple
274+
kcal: 100
275+
- rid: 7418200600248058229
276+
type: {class: Peach, ns: , asm: Assembly-CSharp}
277+
data:
278+
name: Peach
279+
kcal: 100
280+
- rid: 7418200600248058230
281+
type: {class: Grape, ns: , asm: Assembly-CSharp}
282+
data:
283+
name: Grape
284+
kcal: 100
266285
- rid: 9020349853700980763
267286
type: {class: Apple, ns: , asm: Assembly-CSharp}
268287
data:

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,12 @@ public TypePopupCache (AdvancedTypePopup typePopup, AdvancedDropdownState state)
3535

3636
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
3737
{
38-
EditorGUI.BeginProperty(position, label, property);
3938

4039
if (property.propertyType == SerializedPropertyType.ManagedReference)
4140
{
42-
// Render label first to avoid label overlap for lists
43-
Rect foldoutLabelRect = new Rect(position);
44-
foldoutLabelRect.height = EditorGUIUtility.singleLineHeight;
45-
46-
// NOTE: IndentedRect should be disabled as it causes extra indentation.
47-
//foldoutLabelRect = EditorGUI.IndentedRect(foldoutLabelRect);
48-
Rect popupPosition = EditorGUI.PrefixLabel(foldoutLabelRect, label);
49-
5041
#if UNITY_2021_3_OR_NEWER
5142
// Override the label text with the ToString() of the managed reference.
43+
// Must be called before EditorGUI.BeginProperty
5244
var subclassSelectorAttribute = (SubclassSelectorAttribute)attribute;
5345
if (subclassSelectorAttribute.UseToStringAsLabel && !property.hasMultipleDifferentValues)
5446
{
@@ -59,6 +51,15 @@ public override void OnGUI (Rect position, SerializedProperty property, GUIConte
5951
}
6052
}
6153
#endif
54+
55+
EditorGUI.BeginProperty(position, label, property);
56+
// Render label first to avoid label overlap for lists
57+
Rect foldoutLabelRect = new Rect(position);
58+
foldoutLabelRect.height = EditorGUIUtility.singleLineHeight;
59+
60+
// NOTE: IndentedRect should be disabled as it causes extra indentation.
61+
//foldoutLabelRect = EditorGUI.IndentedRect(foldoutLabelRect);
62+
Rect popupPosition = EditorGUI.PrefixLabel(foldoutLabelRect, label);
6263

6364
// Draw the subclass selector popup.
6465
if (EditorGUI.DropdownButton(popupPosition, GetTypeName(property), FocusType.Keyboard))
@@ -129,6 +130,7 @@ public override void OnGUI (Rect position, SerializedProperty property, GUIConte
129130
}
130131
else
131132
{
133+
EditorGUI.BeginProperty(position, label, property);
132134
EditorGUI.LabelField(position, label, IsNotManagedReferenceLabel);
133135
}
134136

0 commit comments

Comments
 (0)