diff --git a/Editor/Drawers/RawReferenceDrawer.cs b/Editor/Drawers/RawReferenceDrawer.cs index 56934f6..9a74735 100644 --- a/Editor/Drawers/RawReferenceDrawer.cs +++ b/Editor/Drawers/RawReferenceDrawer.cs @@ -14,34 +14,10 @@ internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer private object previousReferenceValue; private string previousPropertyPath; - 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 - } - - set - { -#if UNITY_2021_1_OR_NEWER - RawReferenceProperty.managedReferenceValue = value; -#else - fieldInfo.SetValue(Property.serializedObject.targetObject, value); -#endif - } - } - public void Initialize(SerializedProperty property, Type genericType, GUIContent label, FieldInfo fieldInfo) { - Initialize(property, genericType); + Initialize(property, genericType, fieldInfo); this.label = label; - this.fieldInfo = fieldInfo; } /// @@ -89,6 +65,11 @@ public void OnGUI(Rect position) previousPropertyPath = Property.propertyPath; } + 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 599c6ad..1137a98 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; @@ -30,6 +31,67 @@ private enum DragAndDropMode protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference"); protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference"); + protected FieldInfo FieldInfo { get; private set; } + + 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() { CustomObjectDrawer = new CustomObjectDrawer(); @@ -39,10 +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; + FieldInfo = fieldInfo; } private void OnButtonClicked(Rect position) @@ -70,43 +133,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(); + ModeValue = mode; + PropertyValue = reference; } protected abstract void OnPropertiesClicked(); @@ -166,7 +204,7 @@ private void HandleDragUpdated() SetDragAndDropMode(false); return; } - + if (!GenericType.IsAssignableFrom(scriptType)) { SetDragAndDropMode(false); @@ -187,22 +225,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: - if(DragAndDrop.objectReferences[0] is GameObject go) - UnityReferenceProperty.objectReferenceValue = go.GetComponent(GenericType); - else - 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 eefdd83..45a8893 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,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; } @@ -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 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();