Skip to content
Merged
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
13 changes: 12 additions & 1 deletion 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)]
private List<Food> foodsThree = new List<Food>
{
new Apple(),
new Peach(),
new Grape()
};
}

#if UNITY_EDITOR
Expand Down Expand Up @@ -126,4 +137,4 @@ public override float GetPropertyHeight (SerializedProperty property, GUIContent
return EditorGUIUtility.singleLineHeight;
}
}
#endif
#endif
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,9 +35,22 @@ public TypePopupCache (AdvancedTypePopup typePopup, AdvancedDropdownState state)

public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
bool isManagedReference = property.propertyType == SerializedPropertyType.ManagedReference;
#if UNITY_2021_3_OR_NEWER
// Prepare the label before BeginProperty captures it for rendering.
if (isManagedReference && ((SubclassSelectorAttribute)attribute).UseToStringAsLabel && !property.hasMultipleDifferentValues)
{
object managedReferenceValue = property.managedReferenceValue;
if (managedReferenceValue != null)
{
label.text = managedReferenceValue.ToString();
}
}
#endif

EditorGUI.BeginProperty(position, label, property);

if (property.propertyType == SerializedPropertyType.ManagedReference)
if (isManagedReference)
{
// Render label first to avoid label overlap for lists
Rect foldoutLabelRect = new Rect(position);
Expand All @@ -47,19 +60,6 @@ public override void OnGUI (Rect position, SerializedProperty property, GUIConte
//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.
var subclassSelectorAttribute = (SubclassSelectorAttribute)attribute;
if (subclassSelectorAttribute.UseToStringAsLabel && !property.hasMultipleDifferentValues)
{
object managedReferenceValue = property.managedReferenceValue;
if (managedReferenceValue != null)
{
label.text = managedReferenceValue.ToString();
}
}
#endif

// Draw the subclass selector popup.
if (EditorGUI.DropdownButton(popupPosition, GetTypeName(property), FocusType.Keyboard))
{
Expand Down
Loading