From f930d8ed92b8358e417d075f9a089f7161cadc50 Mon Sep 17 00:00:00 2001 From: Christiaan Bloemendaal Date: Fri, 22 Jul 2022 15:43:09 +0200 Subject: [PATCH] refactor: property drawer now reuses cached reference drawers --- Editor/Drawers/RawReferenceDrawer.cs | 13 ++++----- Editor/Drawers/ReferenceDrawer.cs | 16 +++++++---- Editor/Drawers/UnityReferenceDrawer.cs | 6 ++-- Editor/SerializableInterfacePropertyDrawer.cs | 28 +++++++------------ 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/Editor/Drawers/RawReferenceDrawer.cs b/Editor/Drawers/RawReferenceDrawer.cs index c11b9bf..5390f06 100644 --- a/Editor/Drawers/RawReferenceDrawer.cs +++ b/Editor/Drawers/RawReferenceDrawer.cs @@ -8,11 +8,11 @@ namespace TNRD.Drawers { internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer { - private readonly GUIContent label; - private readonly FieldInfo fieldInfo; + private GUIContent label; + private FieldInfo fieldInfo; - private static object previousReferenceValue; - private static string previousPropertyPath; + private object previousReferenceValue; + private string previousPropertyPath; private object RawReferenceValue { @@ -37,10 +37,9 @@ private object RawReferenceValue } } - /// - public RawReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType, FieldInfo fieldInfo) - : base(property, genericType) + public void Initialize(SerializedProperty property, Type genericType, GUIContent label, FieldInfo fieldInfo) { + Initialize(property, genericType); this.label = label; this.fieldInfo = fieldInfo; } diff --git a/Editor/Drawers/ReferenceDrawer.cs b/Editor/Drawers/ReferenceDrawer.cs index 984dc05..0193f43 100644 --- a/Editor/Drawers/ReferenceDrawer.cs +++ b/Editor/Drawers/ReferenceDrawer.cs @@ -21,19 +21,17 @@ private enum DragAndDropMode private DragAndDropMode dragAndDropMode; - protected readonly SerializedProperty Property; - protected readonly Type GenericType; protected readonly CustomObjectDrawer CustomObjectDrawer; + protected SerializedProperty Property { get; private set; } + protected Type GenericType { get; private set; } + protected SerializedProperty ReferenceModeProperty => Property.FindPropertyRelative("mode"); protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference"); protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference"); - protected ReferenceDrawer(SerializedProperty property, Type genericType) + protected ReferenceDrawer() { - Property = property; - GenericType = genericType; - CustomObjectDrawer = new CustomObjectDrawer(); CustomObjectDrawer.ButtonClicked += OnButtonClicked; CustomObjectDrawer.Clicked += OnClicked; @@ -41,6 +39,12 @@ protected ReferenceDrawer(SerializedProperty property, Type genericType) CustomObjectDrawer.PropertiesClicked += OnPropertiesClicked; } + protected void Initialize(SerializedProperty property, Type genericType) + { + Property = property; + GenericType = genericType; + } + private void OnButtonClicked(Rect position) { AdvancedDropdownState state = new AdvancedDropdownState(); diff --git a/Editor/Drawers/UnityReferenceDrawer.cs b/Editor/Drawers/UnityReferenceDrawer.cs index b409799..eefdd83 100644 --- a/Editor/Drawers/UnityReferenceDrawer.cs +++ b/Editor/Drawers/UnityReferenceDrawer.cs @@ -8,11 +8,11 @@ namespace TNRD.Drawers { internal class UnityReferenceDrawer : ReferenceDrawer, IReferenceDrawer { - private readonly GUIContent label; + private GUIContent label; - public UnityReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType) - : base(property, genericType) + public void Initialize(SerializedProperty property, Type genericType, GUIContent label) { + Initialize(property, genericType); this.label = label; } diff --git a/Editor/SerializableInterfacePropertyDrawer.cs b/Editor/SerializableInterfacePropertyDrawer.cs index e71019a..1c9bd0e 100644 --- a/Editor/SerializableInterfacePropertyDrawer.cs +++ b/Editor/SerializableInterfacePropertyDrawer.cs @@ -10,11 +10,12 @@ namespace TNRD [CustomPropertyDrawer(typeof(SerializableInterface<>), true)] internal sealed class SerializableInterfacePropertyDrawer : PropertyDrawer { + private readonly RawReferenceDrawer rawReferenceDrawer = new RawReferenceDrawer(); + private readonly UnityReferenceDrawer unityReferenceDrawer = new UnityReferenceDrawer(); + private SerializedProperty serializedProperty; private Type genericType; - private IReferenceDrawer activeDrawer; - /// public override bool CanCacheInspectorGUI(SerializedProperty property) => false; @@ -23,7 +24,6 @@ private void Initialize(SerializedProperty property) if (serializedProperty == property) return; - activeDrawer = null; serializedProperty = property; genericType = GetGenericArgument(); Assert.IsNotNull(genericType, "Unable to find generic argument, are you doing some shady inheritance?"); @@ -33,16 +33,14 @@ private void Initialize(SerializedProperty property) public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { Initialize(property); - activeDrawer = GetReferenceDrawer(activeDrawer, property, label); - return activeDrawer.GetHeight(); + return GetReferenceDrawer(property, label).GetHeight(); } /// public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { Initialize(property); - activeDrawer = GetReferenceDrawer(activeDrawer, property, label); - activeDrawer.OnGUI(position); + GetReferenceDrawer(property, label).OnGUI(position); } private Type GetGenericArgument() @@ -81,11 +79,7 @@ private Type GetGenericArgument() return null; } - private IReferenceDrawer GetReferenceDrawer( - IReferenceDrawer original, - SerializedProperty property, - GUIContent label - ) + private IReferenceDrawer GetReferenceDrawer(SerializedProperty property, GUIContent label) { SerializedProperty modeProperty = serializedProperty.FindPropertyRelative("mode"); ReferenceMode referenceMode = (ReferenceMode)modeProperty.enumValueIndex; @@ -93,13 +87,11 @@ GUIContent label switch (referenceMode) { case ReferenceMode.Raw: - return original is RawReferenceDrawer - ? original - : new RawReferenceDrawer(property, label, genericType, fieldInfo); + rawReferenceDrawer.Initialize(property, genericType, label, fieldInfo); + return rawReferenceDrawer; case ReferenceMode.Unity: - return original is UnityReferenceDrawer - ? original - : new UnityReferenceDrawer(property, label, genericType); + unityReferenceDrawer.Initialize(property, genericType, label); + return unityReferenceDrawer; default: throw new ArgumentOutOfRangeException(); }