Merge pull request #74 from mackysoft/feature/use-tostring-as-label
Implement SubclassSelectorAttribute.UseToStringAsLabel
This commit is contained in:
commit
60adb3e3d3
@ -64,7 +64,7 @@ public class Example : MonoBehaviour
|
|||||||
[SerializeReference]
|
[SerializeReference]
|
||||||
public Food food3 = new Grape();
|
public Food food3 = new Grape();
|
||||||
|
|
||||||
[SerializeReference, SubclassSelector]
|
[SerializeReference, SubclassSelector(UseToStringAsLabel = true)]
|
||||||
public Food foodOne = new Apple();
|
public Food foodOne = new Apple();
|
||||||
|
|
||||||
[SerializeReference, SubclassSelector]
|
[SerializeReference, SubclassSelector]
|
||||||
|
|||||||
@ -42,6 +42,19 @@ namespace MackySoft.SerializeReferenceExtensions.Editor
|
|||||||
foldoutLabelRect = EditorGUI.IndentedRect(foldoutLabelRect);
|
foldoutLabelRect = EditorGUI.IndentedRect(foldoutLabelRect);
|
||||||
Rect popupPosition = EditorGUI.PrefixLabel(foldoutLabelRect, label);
|
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.
|
// Draw the subclass selector popup.
|
||||||
if (EditorGUI.DropdownButton(popupPosition, GetTypeName(property), FocusType.Keyboard))
|
if (EditorGUI.DropdownButton(popupPosition, GetTypeName(property), FocusType.Keyboard))
|
||||||
{
|
{
|
||||||
|
|||||||
@ -4,7 +4,13 @@ using UnityEngine;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attribute to specify the type of the field serialized by the SerializeReference attribute in the inspector.
|
/// Attribute to specify the type of the field serialized by the SerializeReference attribute in the inspector.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Field,AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
|
||||||
public sealed class SubclassSelectorAttribute : PropertyAttribute {
|
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
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user