diff --git a/Assets/Example/Example.cs b/Assets/Example/Example.cs index f043780..158abbf 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)] + public List foodsThree = new List + { + new Apple(), + new Peach(), + new Grape() + }; } #if UNITY_EDITOR 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..6b6cc52 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs @@ -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) { @@ -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)) @@ -129,6 +130,7 @@ public override void OnGUI (Rect position, SerializedProperty property, GUIConte } else { + EditorGUI.BeginProperty(position, label, property); EditorGUI.LabelField(position, label, IsNotManagedReferenceLabel); }