Skip to content
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
11 changes: 11 additions & 0 deletions Assets/Example/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public abstract class Food
public string name;

public float kcal;

public override string ToString () => $"{name} ({kcal} kcal)";
}

[Serializable]
Expand Down Expand Up @@ -89,6 +91,15 @@ public class Example : MonoBehaviour
new Peach(),
new Grape()
};

// UseToStringAsLabel support on UNITY_2021_3_OR_NEWER
[SerializeReference, SubclassSelector(UseToStringAsLabel = true)]
public List<Food> foodsThree = new List<Food>
{
new Apple(),
new Peach(),
new Grape()
};
}

#if UNITY_EDITOR
Expand Down
19 changes: 19 additions & 0 deletions Assets/Example/Example.unity
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,28 @@ MonoBehaviour:
- rid: 9020349853700980772
- rid: 9020349853700980773
- rid: 9020349853700980774
foodsThree:
- rid: 7418200600248058228
- rid: 7418200600248058229
- rid: 7418200600248058230
references:
version: 2
RefIds:
- rid: 7418200600248058228
type: {class: Apple, ns: , asm: Assembly-CSharp}
data:
name: Apple
kcal: 100
- rid: 7418200600248058229
type: {class: Peach, ns: , asm: Assembly-CSharp}
data:
name: Peach
kcal: 100
- rid: 7418200600248058230
type: {class: Grape, ns: , asm: Assembly-CSharp}
data:
name: Grape
kcal: 100
- rid: 9020349853700980763
type: {class: Apple, ns: , asm: Assembly-CSharp}
data:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,12 @@ public TypePopupCache (AdvancedTypePopup typePopup, AdvancedDropdownState state)

public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);

if (property.propertyType == SerializedPropertyType.ManagedReference)
{
// Render label first to avoid label overlap for lists
Rect foldoutLabelRect = new Rect(position);
foldoutLabelRect.height = EditorGUIUtility.singleLineHeight;

// NOTE: IndentedRect should be disabled as it causes extra indentation.
//foldoutLabelRect = EditorGUI.IndentedRect(foldoutLabelRect);
Rect popupPosition = EditorGUI.PrefixLabel(foldoutLabelRect, label);

#if UNITY_2021_3_OR_NEWER
// Override the label text with the ToString() of the managed reference.
// Must be called before EditorGUI.BeginProperty
var subclassSelectorAttribute = (SubclassSelectorAttribute)attribute;
if (subclassSelectorAttribute.UseToStringAsLabel && !property.hasMultipleDifferentValues)
{
Expand All @@ -59,6 +51,15 @@ public override void OnGUI (Rect position, SerializedProperty property, GUIConte
}
}
#endif

EditorGUI.BeginProperty(position, label, property);
// Render label first to avoid label overlap for lists
Rect foldoutLabelRect = new Rect(position);
foldoutLabelRect.height = EditorGUIUtility.singleLineHeight;

// NOTE: IndentedRect should be disabled as it causes extra indentation.
//foldoutLabelRect = EditorGUI.IndentedRect(foldoutLabelRect);
Rect popupPosition = EditorGUI.PrefixLabel(foldoutLabelRect, label);

// Draw the subclass selector popup.
if (EditorGUI.DropdownButton(popupPosition, GetTypeName(property), FocusType.Keyboard))
Expand Down Expand Up @@ -129,6 +130,7 @@ public override void OnGUI (Rect position, SerializedProperty property, GUIConte
}
else
{
EditorGUI.BeginProperty(position, label, property);
EditorGUI.LabelField(position, label, IsNotManagedReferenceLabel);
}

Expand Down
Loading