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
2 changes: 1 addition & 1 deletion Assets/Example/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
/// <summary>
/// Attribute to specify the type of the field serialized by the SerializeReference attribute in the inspector.
/// </summary>
[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

}