From 17040d07e8ba69247e55dd5adca658813d43635a Mon Sep 17 00:00:00 2001 From: Marc-Antoine Girard Date: Tue, 12 Jul 2022 14:12:45 -0400 Subject: [PATCH 1/2] Added proper properties in ReferenceDrawer.cs --- Editor/Drawers/RawReferenceDrawer.cs | 23 +--- Editor/Drawers/ReferenceDrawer.cs | 109 ++++++++++++------ Editor/Drawers/UnityReferenceDrawer.cs | 10 +- Editor/SerializableInterfacePropertyDrawer.cs | 2 +- 4 files changed, 89 insertions(+), 55 deletions(-) diff --git a/Editor/Drawers/RawReferenceDrawer.cs b/Editor/Drawers/RawReferenceDrawer.cs index 72fa6e7..5251d15 100644 --- a/Editor/Drawers/RawReferenceDrawer.cs +++ b/Editor/Drawers/RawReferenceDrawer.cs @@ -9,28 +9,12 @@ namespace TNRD.Drawers internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer { private readonly GUIContent label; - private readonly FieldInfo fieldInfo; - - private object RawReferenceValue - { - get - { -#if UNITY_2021_1_OR_NEWER - return RawReferenceProperty.managedReferenceValue; -#else - ISerializableInterface instance = - (ISerializableInterface)fieldInfo.GetValue(Property.serializedObject.targetObject); - return instance.GetRawReference(); -#endif - } - } /// public RawReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType, FieldInfo fieldInfo) - : base(property, genericType) + : base(property, genericType, fieldInfo) { this.label = label; - this.fieldInfo = fieldInfo; } /// @@ -73,6 +57,11 @@ public void OnGUI(Rect position) HandleDragAndDrop(objectFieldRect); } + protected override void PingObject() + { + // No support for pinging raw objects for now (I guess this would ping the MonoScript?) + } + /// protected override void OnPropertiesClicked() { diff --git a/Editor/Drawers/ReferenceDrawer.cs b/Editor/Drawers/ReferenceDrawer.cs index 984dc05..a2dff79 100644 --- a/Editor/Drawers/ReferenceDrawer.cs +++ b/Editor/Drawers/ReferenceDrawer.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Reflection; using TNRD.Utilities; using UnityEditor; using UnityEditor.IMGUI.Controls; @@ -29,10 +30,72 @@ private enum DragAndDropMode protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference"); protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference"); - protected ReferenceDrawer(SerializedProperty property, Type genericType) + protected readonly FieldInfo fieldInfo; + + protected ReferenceMode ModeValue + { + get => (ReferenceMode)ReferenceModeProperty.enumValueIndex; + set => ReferenceModeProperty.enumValueIndex = (int)value; + } + + protected object RawReferenceValue + { + get + { +#if UNITY_2021_1_OR_NEWER + return RawReferenceProperty.managedReferenceValue; +#else + ISerializableInterface instance = + (ISerializableInterface)fieldInfo.GetValue(Property.serializedObject.targetObject); + return instance.GetRawReference(); +#endif + } + set + { +#if UNITY_2021_1_OR_NEWER + RawReferenceProperty.managedReferenceValue = value; +#else + fieldInfo.SetValue(Property.serializedObject.targetObject, value); +#endif + } + } + + protected object PropertyValue + { + get + { + return ModeValue switch + { + ReferenceMode.Raw => RawReferenceValue, + ReferenceMode.Unity => UnityReferenceProperty.objectReferenceValue, + _ => throw new ArgumentOutOfRangeException() + }; + } + set + { + switch (ModeValue) + { + case ReferenceMode.Raw: + RawReferenceValue = value; + UnityReferenceProperty.objectReferenceValue = null; + break; + case ReferenceMode.Unity: + UnityReferenceProperty.objectReferenceValue = (Object)value; + RawReferenceValue = null; + break; + default: + throw new ArgumentOutOfRangeException(); + } + + Property.serializedObject.ApplyModifiedProperties(); + } + } + + protected ReferenceDrawer(SerializedProperty property, Type genericType, FieldInfo fieldInfo) { Property = property; GenericType = genericType; + this.fieldInfo = fieldInfo; CustomObjectDrawer = new CustomObjectDrawer(); CustomObjectDrawer.ButtonClicked += OnButtonClicked; @@ -66,43 +129,18 @@ private void OnButtonClicked(Rect position) private void OnClicked() { - switch ((ReferenceMode)ReferenceModeProperty.enumValueIndex) - { - case ReferenceMode.Raw: - // No support for pinging raw objects for now (I guess this would ping the MonoScript?) - break; - case ReferenceMode.Unity: - EditorGUIUtility.PingObject(UnityReferenceProperty.objectReferenceValue); - break; - default: - throw new ArgumentOutOfRangeException(); - } + PingObject(); } private void OnDeletePressed() { - RawReferenceProperty.managedReferenceValue = null; - UnityReferenceProperty.objectReferenceValue = null; - Property.serializedObject.ApplyModifiedProperties(); + PropertyValue = null; } private void OnItemSelected(ReferenceMode mode, object reference) { ReferenceModeProperty.enumValueIndex = (int)mode; - - switch (mode) - { - case ReferenceMode.Raw: - RawReferenceProperty.managedReferenceValue = reference; - break; - case ReferenceMode.Unity: - UnityReferenceProperty.objectReferenceValue = (Object)reference; - break; - default: - throw new ArgumentOutOfRangeException(nameof(mode), mode, null); - } - - Property.serializedObject.ApplyModifiedProperties(); + PropertyValue = reference; } protected abstract void OnPropertiesClicked(); @@ -161,7 +199,7 @@ private void HandleDragUpdated(Rect position) SetDragAndDropMode(false); return; } - + if (!GenericType.IsAssignableFrom(scriptType)) { SetDragAndDropMode(false); @@ -182,18 +220,19 @@ private void HandleDragPerform() switch (dragAndDropMode) { case DragAndDropMode.Raw: - RawReferenceProperty.managedReferenceValue = - Activator.CreateInstance(((MonoScript)DragAndDrop.objectReferences[0]).GetClass()); - ReferenceModeProperty.enumValueIndex = (int)ReferenceMode.Raw; + ModeValue = ReferenceMode.Raw; + PropertyValue = Activator.CreateInstance(((MonoScript)DragAndDrop.objectReferences[0]).GetClass()); break; case DragAndDropMode.Unity: - UnityReferenceProperty.objectReferenceValue = DragAndDrop.objectReferences[0]; - ReferenceModeProperty.enumValueIndex = (int)ReferenceMode.Unity; + ModeValue = ReferenceMode.Unity; + PropertyValue = DragAndDrop.objectReferences[0]; break; case DragAndDropMode.None: default: throw new ArgumentOutOfRangeException(); } } + + protected abstract void PingObject(); } } diff --git a/Editor/Drawers/UnityReferenceDrawer.cs b/Editor/Drawers/UnityReferenceDrawer.cs index b409799..8592c49 100644 --- a/Editor/Drawers/UnityReferenceDrawer.cs +++ b/Editor/Drawers/UnityReferenceDrawer.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection; using TNRD.Utilities; using UnityEditor; using UnityEngine; @@ -10,8 +11,8 @@ internal class UnityReferenceDrawer : ReferenceDrawer, IReferenceDrawer { private readonly GUIContent label; - public UnityReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType) - : base(property, genericType) + public UnityReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType, FieldInfo fieldInfo) + : base(property, genericType, fieldInfo) { this.label = label; } @@ -30,6 +31,11 @@ public void OnGUI(Rect position) HandleDragAndDrop(position); } + protected override void PingObject() + { + EditorGUIUtility.PingObject((Object)PropertyValue); + } + protected override void OnPropertiesClicked() { PropertyEditorUtility.Show(UnityReferenceProperty.objectReferenceValue); diff --git a/Editor/SerializableInterfacePropertyDrawer.cs b/Editor/SerializableInterfacePropertyDrawer.cs index 7ce4953..154c1f7 100644 --- a/Editor/SerializableInterfacePropertyDrawer.cs +++ b/Editor/SerializableInterfacePropertyDrawer.cs @@ -102,7 +102,7 @@ GUIContent label case ReferenceMode.Unity: return original is UnityReferenceDrawer ? original - : new UnityReferenceDrawer(property, label, genericType); + : new UnityReferenceDrawer(property, label, genericType, fieldInfo); default: throw new ArgumentOutOfRangeException(); } From 6ddab6ec21ba342403b5dca2294a06743ae510e7 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Girard Date: Fri, 22 Jul 2022 11:43:05 -0400 Subject: [PATCH 2/2] Fixed Merge Conflict --- Editor/Drawers/RawReferenceDrawer.cs | 2 +- Editor/Drawers/ReferenceDrawer.cs | 12 ++++++------ Editor/Drawers/UnityReferenceDrawer.cs | 4 ++-- Editor/SerializableInterfacePropertyDrawer.cs | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Editor/Drawers/RawReferenceDrawer.cs b/Editor/Drawers/RawReferenceDrawer.cs index 671fb68..a63f2a7 100644 --- a/Editor/Drawers/RawReferenceDrawer.cs +++ b/Editor/Drawers/RawReferenceDrawer.cs @@ -16,7 +16,7 @@ internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer public void Initialize(SerializedProperty property, Type genericType, GUIContent label, FieldInfo fieldInfo) { - Initialize(property, genericType); + Initialize(property, genericType, fieldInfo); this.label = label; } diff --git a/Editor/Drawers/ReferenceDrawer.cs b/Editor/Drawers/ReferenceDrawer.cs index 8d2d913..9d20ac5 100644 --- a/Editor/Drawers/ReferenceDrawer.cs +++ b/Editor/Drawers/ReferenceDrawer.cs @@ -31,7 +31,7 @@ private enum DragAndDropMode protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference"); protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference"); - protected readonly FieldInfo fieldInfo; + protected FieldInfo FieldInfo { get; private set; } protected ReferenceMode ModeValue { @@ -47,7 +47,7 @@ protected object RawReferenceValue return RawReferenceProperty.managedReferenceValue; #else ISerializableInterface instance = - (ISerializableInterface)fieldInfo.GetValue(Property.serializedObject.targetObject); + (ISerializableInterface)FieldInfo.GetValue(Property.serializedObject.targetObject); return instance.GetRawReference(); #endif } @@ -56,7 +56,7 @@ protected object RawReferenceValue #if UNITY_2021_1_OR_NEWER RawReferenceProperty.managedReferenceValue = value; #else - fieldInfo.SetValue(Property.serializedObject.targetObject, value); + FieldInfo.SetValue(Property.serializedObject.targetObject, value); #endif } } @@ -101,11 +101,11 @@ protected ReferenceDrawer() CustomObjectDrawer.PropertiesClicked += OnPropertiesClicked; } - protected void Initialize(SerializedProperty property, Type genericType) + protected void Initialize(SerializedProperty property, Type genericType, FieldInfo fieldInfo) { Property = property; GenericType = genericType; - this.fieldInfo = fieldInfo; + FieldInfo = fieldInfo; } private void OnButtonClicked(Rect position) @@ -143,7 +143,7 @@ private void OnDeletePressed() private void OnItemSelected(ReferenceMode mode, object reference) { - ReferenceModeProperty.enumValueIndex = (int)mode; + ModeValue = mode; PropertyValue = reference; } diff --git a/Editor/Drawers/UnityReferenceDrawer.cs b/Editor/Drawers/UnityReferenceDrawer.cs index fb2b07d..45a8893 100644 --- a/Editor/Drawers/UnityReferenceDrawer.cs +++ b/Editor/Drawers/UnityReferenceDrawer.cs @@ -11,9 +11,9 @@ internal class UnityReferenceDrawer : ReferenceDrawer, IReferenceDrawer { private GUIContent label; - public void Initialize(SerializedProperty property, Type genericType, GUIContent label) + public void Initialize(SerializedProperty property, Type genericType, GUIContent label, FieldInfo fieldInfo) { - Initialize(property, genericType); + Initialize(property, genericType, fieldInfo); this.label = label; } diff --git a/Editor/SerializableInterfacePropertyDrawer.cs b/Editor/SerializableInterfacePropertyDrawer.cs index 1c9bd0e..3d2c9aa 100644 --- a/Editor/SerializableInterfacePropertyDrawer.cs +++ b/Editor/SerializableInterfacePropertyDrawer.cs @@ -90,7 +90,7 @@ private IReferenceDrawer GetReferenceDrawer(SerializedProperty property, GUICont rawReferenceDrawer.Initialize(property, genericType, label, fieldInfo); return rawReferenceDrawer; case ReferenceMode.Unity: - unityReferenceDrawer.Initialize(property, genericType, label); + unityReferenceDrawer.Initialize(property, genericType, label, fieldInfo); return unityReferenceDrawer; default: throw new ArgumentOutOfRangeException();