From 693da105b2bb6342d89334a8838a881e2add2c0b Mon Sep 17 00:00:00 2001 From: Marc-Antoine Girard Date: Tue, 12 Jul 2022 16:09:12 -0400 Subject: [PATCH 1/6] Update RawReferenceDrawer.cs --- Editor/Drawers/RawReferenceDrawer.cs | 30 +--------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/Editor/Drawers/RawReferenceDrawer.cs b/Editor/Drawers/RawReferenceDrawer.cs index 893581f..1c42b30 100644 --- a/Editor/Drawers/RawReferenceDrawer.cs +++ b/Editor/Drawers/RawReferenceDrawer.cs @@ -9,34 +9,10 @@ namespace TNRD.Drawers internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer { private readonly GUIContent label; - private readonly FieldInfo fieldInfo; private static object previousReferenceValue; private static 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 RawReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType, FieldInfo fieldInfo) : base(property, genericType, fieldInfo) @@ -125,17 +101,13 @@ private void AvoidDuplicateReferencesInArray() if (previousPropertyPath == Property.propertyPath) return; - SerializedProperty rawReferenceProperty = Property.FindPropertyRelative("rawReference"); object currentReferenceValue = RawReferenceValue; if (currentReferenceValue == null) return; if (previousReferenceValue == currentReferenceValue) - { - RawReferenceValue = CreateInstance(currentReferenceValue); - rawReferenceProperty.serializedObject.ApplyModifiedProperties(); - } + PropertyValue = CreateInstance(currentReferenceValue); previousReferenceValue = currentReferenceValue; } From 64c42a4e1294183f84bb7af7f3ede9662432e89d Mon Sep 17 00:00:00 2001 From: Marc-Antoine Girard Date: Tue, 12 Jul 2022 18:21:54 -0400 Subject: [PATCH 2/6] Update ReferenceDrawer.cs --- Editor/Drawers/ReferenceDrawer.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Editor/Drawers/ReferenceDrawer.cs b/Editor/Drawers/ReferenceDrawer.cs index 54166d6..44198db 100644 --- a/Editor/Drawers/ReferenceDrawer.cs +++ b/Editor/Drawers/ReferenceDrawer.cs @@ -226,7 +226,7 @@ private void HandleDragPerform() break; case DragAndDropMode.Unity: ModeValue = ReferenceMode.Unity; - PropertyValue = DragAndDrop.objectReferences[0]; + PropertyValue = GetUnityObject(DragAndDrop.objectReferences[0]); break; case DragAndDropMode.None: default: @@ -234,6 +234,13 @@ private void HandleDragPerform() } } + private Object GetUnityObject(Object objectReference) + { + if(objectReference is GameObject gameObject) + return gameObject.GetComponent(GenericType); + return objectReference; + } + protected abstract void PingObject(); } } From 5c4525162ceb18a02db530ac5bc8c4cafaf0e36d Mon Sep 17 00:00:00 2001 From: Marc-Antoine Girard Date: Tue, 12 Jul 2022 18:35:05 -0400 Subject: [PATCH 3/6] Forgot to set a ModeValue --- Editor/Drawers/ReferenceDrawer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Editor/Drawers/ReferenceDrawer.cs b/Editor/Drawers/ReferenceDrawer.cs index 44198db..823169d 100644 --- a/Editor/Drawers/ReferenceDrawer.cs +++ b/Editor/Drawers/ReferenceDrawer.cs @@ -139,7 +139,7 @@ private void OnDeletePressed() private void OnItemSelected(ReferenceMode mode, object reference) { - ReferenceModeProperty.enumValueIndex = (int)mode; + ModeValue = mode; PropertyValue = reference; } @@ -195,7 +195,7 @@ private void HandleDragUpdated() { Type scriptType = monoScript.GetClass(); - if (scriptType.IsSubclassOf(typeof(UnityEngine.Object))) + if (scriptType.IsSubclassOf(typeof(Object))) { SetDragAndDropMode(false); return; From a400f57d1b0bdb97ba0670597fc4a235518c3b99 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Girard Date: Thu, 14 Jul 2022 15:50:36 -0400 Subject: [PATCH 4/6] Update ReferenceDrawer.cs PropertyValue setter Should always check if gameObject. This will be useful later on. --- Editor/Drawers/ReferenceDrawer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Editor/Drawers/ReferenceDrawer.cs b/Editor/Drawers/ReferenceDrawer.cs index 823169d..a64eb99 100644 --- a/Editor/Drawers/ReferenceDrawer.cs +++ b/Editor/Drawers/ReferenceDrawer.cs @@ -80,7 +80,7 @@ protected object PropertyValue UnityReferenceProperty.objectReferenceValue = null; break; case ReferenceMode.Unity: - UnityReferenceProperty.objectReferenceValue = (Object)value; + UnityReferenceProperty.objectReferenceValue = GetUnityObject((Object)value); RawReferenceValue = null; break; default: @@ -226,7 +226,7 @@ private void HandleDragPerform() break; case DragAndDropMode.Unity: ModeValue = ReferenceMode.Unity; - PropertyValue = GetUnityObject(DragAndDrop.objectReferences[0]); + PropertyValue = DragAndDrop.objectReferences[0]; break; case DragAndDropMode.None: default: From 1610d5f2342446f576683c7ea4c9caf7e3d141a5 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Girard Date: Thu, 14 Jul 2022 16:24:03 -0400 Subject: [PATCH 5/6] Update AssetsItemBuilder.cs --- Editor/Builders/AssetsItemBuilder.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Editor/Builders/AssetsItemBuilder.cs b/Editor/Builders/AssetsItemBuilder.cs index 03dff70..dd1fe70 100644 --- a/Editor/Builders/AssetsItemBuilder.cs +++ b/Editor/Builders/AssetsItemBuilder.cs @@ -4,6 +4,7 @@ using TNRD.Utilities; using UnityEditor; using UnityEditor.IMGUI.Controls; +using UnityEngine; using UnityEngine.Assertions; namespace TNRD.Builders @@ -37,6 +38,12 @@ public AdvancedDropdownItem Build() { CreateItemForPath(root, assetPath); } + else if (assetType == typeof(GameObject)) + { + GameObject gameObject = AssetDatabase.LoadAssetAtPath(assetPath); + if (gameObject.GetComponent(interfaceType) != null) + CreateItemForPath(root, assetPath); + } } return root; From cbd619412192b6d99f8c06737af7903b6626daa1 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Girard Date: Fri, 22 Jul 2022 14:04:19 -0400 Subject: [PATCH 6/6] Fix issues during merge --- Editor/Drawers/RawReferenceDrawer.cs | 25 +------------------------ Editor/Drawers/ReferenceDrawer.cs | 10 ++++++---- Editor/Drawers/UnityReferenceDrawer.cs | 4 ++-- 3 files changed, 9 insertions(+), 30 deletions(-) diff --git a/Editor/Drawers/RawReferenceDrawer.cs b/Editor/Drawers/RawReferenceDrawer.cs index 79db041..bbb32bb 100644 --- a/Editor/Drawers/RawReferenceDrawer.cs +++ b/Editor/Drawers/RawReferenceDrawer.cs @@ -14,32 +14,9 @@ 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; } diff --git a/Editor/Drawers/ReferenceDrawer.cs b/Editor/Drawers/ReferenceDrawer.cs index b206aab..56456fd 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,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) @@ -137,6 +138,7 @@ private void OnClicked() private void OnDeletePressed() { + ModeValue = default; PropertyValue = null; } 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; }