Skip to content

Commit e61a077

Browse files
committed
2 parents 2dfbdc0 + 60adb3e commit e61a077

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Assets/Example/Example.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class Example : MonoBehaviour
6464
[SerializeReference]
6565
public Food food3 = new Grape();
6666

67-
[SerializeReference, SubclassSelector]
67+
[SerializeReference, SubclassSelector(UseToStringAsLabel = true)]
6868
public Food foodOne = new Apple();
6969

7070
[SerializeReference, SubclassSelector]

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ public override void OnGUI (Rect position, SerializedProperty property, GUIConte
4242
foldoutLabelRect = EditorGUI.IndentedRect(foldoutLabelRect);
4343
Rect popupPosition = EditorGUI.PrefixLabel(foldoutLabelRect, label);
4444

45+
#if UNITY_2021_3_OR_NEWER
46+
// Override the label text with the ToString() of the managed reference.
47+
var subclassSelectorAttribute = (SubclassSelectorAttribute)attribute;
48+
if (subclassSelectorAttribute.UseToStringAsLabel && !property.hasMultipleDifferentValues)
49+
{
50+
object managedReferenceValue = property.managedReferenceValue;
51+
if (managedReferenceValue != null)
52+
{
53+
label.text = managedReferenceValue.ToString();
54+
}
55+
}
56+
#endif
57+
4558
// Draw the subclass selector popup.
4659
if (EditorGUI.DropdownButton(popupPosition, GetTypeName(property), FocusType.Keyboard))
4760
{

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Runtime/SubclassSelectorAttribute.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
/// <summary>
55
/// Attribute to specify the type of the field serialized by the SerializeReference attribute in the inspector.
66
/// </summary>
7-
[AttributeUsage(AttributeTargets.Field,AllowMultiple = false)]
8-
public sealed class SubclassSelectorAttribute : PropertyAttribute {
9-
7+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
8+
public sealed class SubclassSelectorAttribute : PropertyAttribute
9+
{
10+
11+
#if UNITY_2021_3_OR_NEWER
12+
// NOTE: Use managedReferenceValue getter to invoke instance method in SubclassSelectorDrawer.
13+
public bool UseToStringAsLabel { get; set; }
14+
#endif
15+
1016
}

0 commit comments

Comments
 (0)