diff --git a/Assets/Example/Example.cs b/Assets/Example/Example.cs index f043780..47981e9 100644 --- a/Assets/Example/Example.cs +++ b/Assets/Example/Example.cs @@ -9,6 +9,8 @@ public abstract class Food public string name; public float kcal; + + public override string ToString () => $"{name} ({kcal} kcal)"; } [Serializable] @@ -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 foodsThree = new List + { + new Apple(), + new Peach(), + new Grape() + }; } #if UNITY_EDITOR @@ -126,4 +137,4 @@ public override float GetPropertyHeight (SerializedProperty property, GUIContent return EditorGUIUtility.singleLineHeight; } } -#endif \ No newline at end of file +#endif diff --git a/Assets/Example/Example.unity b/Assets/Example/Example.unity index 09287f1..4cb36d4 100644 --- a/Assets/Example/Example.unity +++ b/Assets/Example/Example.unity @@ -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: diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs index d4b787f..f1967e6 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs @@ -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); @@ -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)) {