diff --git a/Assets/Example/Example.cs b/Assets/Example/Example.cs index 71efede..e0eb620 100644 --- a/Assets/Example/Example.cs +++ b/Assets/Example/Example.cs @@ -64,7 +64,7 @@ public class Example : MonoBehaviour [SerializeReference] public Food food3 = new Grape(); - [SerializeReference, SubclassSelector] + [SerializeReference, SubclassSelector(UseToStringAsLabel = true)] public Food foodOne = new Apple(); [SerializeReference, SubclassSelector] diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs index 365ab9a..8c0e3f3 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs @@ -42,6 +42,19 @@ namespace MackySoft.SerializeReferenceExtensions.Editor 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)) { diff --git a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Runtime/SubclassSelectorAttribute.cs b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Runtime/SubclassSelectorAttribute.cs index 8692fe7..ad78e8f 100644 --- a/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Runtime/SubclassSelectorAttribute.cs +++ b/Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Runtime/SubclassSelectorAttribute.cs @@ -4,7 +4,13 @@ using UnityEngine; /// /// Attribute to specify the type of the field serialized by the SerializeReference attribute in the inspector. /// -[AttributeUsage(AttributeTargets.Field,AllowMultiple = false)] -public sealed class SubclassSelectorAttribute : PropertyAttribute { - +[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] +public sealed class SubclassSelectorAttribute : PropertyAttribute +{ + +#if UNITY_2021_3_OR_NEWER + // NOTE: Use managedReferenceValue getter to invoke instance method in SubclassSelectorDrawer. + public bool UseToStringAsLabel { get; set; } +#endif + } \ No newline at end of file