diff --git a/Editor/Drawers.meta b/Editor/Drawers.meta new file mode 100644 index 0000000..4c15c15 --- /dev/null +++ b/Editor/Drawers.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 092a8a00426d46aba03d829da3c59d8e +timeCreated: 1651172005 \ No newline at end of file diff --git a/Editor/SerializableInterfacePropertyDrawer.Styles.cs b/Editor/Drawers/CustomObjectDrawer.Styles.cs similarity index 94% rename from Editor/SerializableInterfacePropertyDrawer.Styles.cs rename to Editor/Drawers/CustomObjectDrawer.Styles.cs index 18f2d22..49af286 100644 --- a/Editor/SerializableInterfacePropertyDrawer.Styles.cs +++ b/Editor/Drawers/CustomObjectDrawer.Styles.cs @@ -1,9 +1,9 @@ using UnityEditor; using UnityEngine; -namespace TNRD +namespace TNRD.Drawers { - internal sealed partial class SerializableInterfacePropertyDrawer + public partial class CustomObjectDrawer { private static class Styles { diff --git a/Editor/Drawers/CustomObjectDrawer.Styles.cs.meta b/Editor/Drawers/CustomObjectDrawer.Styles.cs.meta new file mode 100644 index 0000000..4ac246e --- /dev/null +++ b/Editor/Drawers/CustomObjectDrawer.Styles.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: dca8b4d8187c486bae8950c504dde4e4 +timeCreated: 1651176627 \ No newline at end of file diff --git a/Editor/Drawers/CustomObjectDrawer.cs b/Editor/Drawers/CustomObjectDrawer.cs new file mode 100644 index 0000000..cdd2e30 --- /dev/null +++ b/Editor/Drawers/CustomObjectDrawer.cs @@ -0,0 +1,114 @@ +using UnityEditor; +using UnityEngine; + +namespace TNRD.Drawers +{ + public partial class CustomObjectDrawer + { + public delegate void ButtonClickedDelegate(Rect position); + + public delegate void ClickedDelegate(); + + public delegate void DeletePressedDelegate(); + + public delegate void PropertiesClickedDelegate(); + + private bool isSelected; + + private Event Event => Event.current; + + public event ButtonClickedDelegate ButtonClicked; + public event ClickedDelegate Clicked; + public event DeletePressedDelegate DeletePressed; + public event PropertiesClickedDelegate PropertiesClicked; + + public void OnGUI(Rect position, GUIContent label, GUIContent content) + { + Rect positionWithoutThumb = new Rect(position); + positionWithoutThumb.xMax -= 20; + + position = DrawPrefixLabel(position, label); + DrawObjectField(position, content); + DrawButton(position); + + HandleMouseDown(position, positionWithoutThumb); + HandleKeyDown(); + } + + private Rect DrawPrefixLabel(Rect position, GUIContent label) + { + GUIStyle labelStyle = isSelected ? Styles.SelectedLabelStyle : Styles.RegularLabelStyle; + Rect result = EditorGUI.PrefixLabel(position, label, labelStyle); + return result; + } + + private void DrawObjectField(Rect position, GUIContent objectFieldContent) + { + Rect positionWithoutThumb = new Rect(position); + positionWithoutThumb.xMax -= 20; + + if (Event.type == EventType.Repaint) + { + EditorStyles.objectField.Draw(position, + objectFieldContent, + position.Contains(Event.mousePosition), + false, + false, + isSelected); + } + } + + private void ForceRepaintEditors() + { + foreach (Editor activeEditor in ActiveEditorTracker.sharedTracker.activeEditors) + { + activeEditor.Repaint(); + } + } + + private void DrawButton(Rect position) + { + Rect buttonRect = new Rect(position); + buttonRect.yMin += 1; + buttonRect.yMax -= 1; + buttonRect.xMin = buttonRect.xMax - 20; + buttonRect.xMax -= 1; + + if (GUI.Button(buttonRect, string.Empty, "objectFieldButton")) + { + ButtonClicked?.Invoke(position); + } + } + + private void HandleMouseDown(Rect position, Rect positionWithoutThumb) + { + if (Event.type != EventType.MouseDown) + return; + + if (Event.button == 0) + { + isSelected = positionWithoutThumb.Contains(Event.mousePosition); + ForceRepaintEditors(); + Clicked?.Invoke(); + } + else if (Event.button == 1 && positionWithoutThumb.Contains(Event.mousePosition)) + { + GenericMenu menu = new GenericMenu(); + menu.AddItem(new GUIContent("Properties..."), false, () => { PropertiesClicked?.Invoke(); }); + menu.DropDown(position); + Event.Use(); + } + } + + private void HandleKeyDown() + { + if (!isSelected) + return; + + if (Event.type == EventType.KeyDown && Event.keyCode == KeyCode.Delete) + { + DeletePressed?.Invoke(); + } + } + } +} \ No newline at end of file diff --git a/Editor/Drawers/CustomObjectDrawer.cs.meta b/Editor/Drawers/CustomObjectDrawer.cs.meta new file mode 100644 index 0000000..3a1d559 --- /dev/null +++ b/Editor/Drawers/CustomObjectDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d6ed901070c64bfcaa7de131652bdd40 +timeCreated: 1651172254 \ No newline at end of file diff --git a/Editor/Drawers/IReferenceDrawer.cs b/Editor/Drawers/IReferenceDrawer.cs new file mode 100644 index 0000000..798e658 --- /dev/null +++ b/Editor/Drawers/IReferenceDrawer.cs @@ -0,0 +1,10 @@ +using UnityEngine; + +namespace TNRD.Drawers +{ + internal interface IReferenceDrawer + { + float GetHeight(); + void OnGUI(Rect position); + } +} \ No newline at end of file diff --git a/Editor/Drawers/IReferenceDrawer.cs.meta b/Editor/Drawers/IReferenceDrawer.cs.meta new file mode 100644 index 0000000..49566a6 --- /dev/null +++ b/Editor/Drawers/IReferenceDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c88f02b7e9174967b1bc0670049f006f +timeCreated: 1651174294 \ No newline at end of file diff --git a/Editor/Drawers/RawReferenceDrawer.cs b/Editor/Drawers/RawReferenceDrawer.cs new file mode 100644 index 0000000..c408e2e --- /dev/null +++ b/Editor/Drawers/RawReferenceDrawer.cs @@ -0,0 +1,92 @@ +using System; +using System.Reflection; +using UnityEditor; +using UnityEngine; + +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) + { + this.label = label; + this.fieldInfo = fieldInfo; + } + + /// + public float GetHeight() + { + if (RawReferenceValue == null) + return EditorGUIUtility.singleLineHeight; + + return EditorGUIUtility.singleLineHeight + + EditorGUIUtility.standardVerticalSpacing + + EditorGUI.GetPropertyHeight(RawReferenceProperty, true); + } + + /// + public void OnGUI(Rect position) + { + Rect objectFieldRect = new Rect(position) + { + height = EditorGUIUtility.singleLineHeight + }; + + object rawReferenceValue = RawReferenceValue; + + GUIContent content = rawReferenceValue == null + ? EditorGUIUtility.ObjectContent((MonoScript)null, typeof(MonoScript)) + : new GUIContent(rawReferenceValue.GetType().Name, IconUtility.ScriptIcon); + + CustomObjectDrawer.OnGUI(objectFieldRect, label, content); + if (rawReferenceValue == null) + return; + + Rect objectDrawerRect = new Rect(position); + objectDrawerRect.yMin += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + + EditorGUI.PropertyField(objectDrawerRect, + RawReferenceProperty, + new GUIContent(rawReferenceValue.GetType().Name), + true); + } + + /// + protected override void OnPropertiesClicked() + { + Type type = RawReferenceValue.GetType(); + string typeName = type.Name; + + string[] guids = AssetDatabase.FindAssets($"t:Script {typeName}"); + foreach (string guid in guids) + { + string assetPath = AssetDatabase.GUIDToAssetPath(guid); + MonoScript monoScript = AssetDatabase.LoadAssetAtPath(assetPath); + if (monoScript.GetClass() == type) + { + PropertyEditorUtility.Show(monoScript); + return; + } + } + } + } +} \ No newline at end of file diff --git a/Editor/Drawers/RawReferenceDrawer.cs.meta b/Editor/Drawers/RawReferenceDrawer.cs.meta new file mode 100644 index 0000000..86592a9 --- /dev/null +++ b/Editor/Drawers/RawReferenceDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fe6e67367395478ea805d0231da3a352 +timeCreated: 1651174357 \ No newline at end of file diff --git a/Editor/Drawers/ReferenceDrawer.cs b/Editor/Drawers/ReferenceDrawer.cs new file mode 100644 index 0000000..647ae14 --- /dev/null +++ b/Editor/Drawers/ReferenceDrawer.cs @@ -0,0 +1,98 @@ +using System; +using UnityEditor; +using UnityEditor.IMGUI.Controls; +using UnityEngine; +using UnityEngine.SceneManagement; +using Object = UnityEngine.Object; + +namespace TNRD.Drawers +{ + internal abstract class ReferenceDrawer + { + protected readonly SerializedProperty Property; + protected readonly Type GenericType; + protected readonly CustomObjectDrawer CustomObjectDrawer; + + protected SerializedProperty ReferenceModeProperty => Property.FindPropertyRelative("mode"); + protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference"); + protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference"); + + protected ReferenceDrawer(SerializedProperty property, Type genericType) + { + Property = property; + GenericType = genericType; + + CustomObjectDrawer = new CustomObjectDrawer(); + CustomObjectDrawer.ButtonClicked += OnButtonClicked; + CustomObjectDrawer.Clicked += OnClicked; + CustomObjectDrawer.DeletePressed += OnDeletePressed; + CustomObjectDrawer.PropertiesClicked += OnPropertiesClicked; + } + + private void OnButtonClicked(Rect position) + { + AdvancedDropdownState state = new AdvancedDropdownState(); + SerializableInterfaceAdvancedDropdown dropdown = + new SerializableInterfaceAdvancedDropdown(state, GenericType, GetRelevantScene()); + dropdown.ItemSelectedEvent += OnItemSelected; + dropdown.Show(position); + } + + private Scene? GetRelevantScene() + { + Object target = Property.serializedObject.targetObject; + + if (target is ScriptableObject) + return null; + if (target is Component component) + return component.gameObject.scene; + if (target is GameObject gameObject) + return gameObject.scene; + + return null; + } + + 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(); + } + } + + private void OnDeletePressed() + { + RawReferenceProperty.managedReferenceValue = null; + UnityReferenceProperty.objectReferenceValue = null; + Property.serializedObject.ApplyModifiedProperties(); + } + + 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(); + } + + protected abstract void OnPropertiesClicked(); + } +} \ No newline at end of file diff --git a/Editor/Drawers/ReferenceDrawer.cs.meta b/Editor/Drawers/ReferenceDrawer.cs.meta new file mode 100644 index 0000000..df37c33 --- /dev/null +++ b/Editor/Drawers/ReferenceDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c1803d06a9a64d43ae15f1a5b898ae15 +timeCreated: 1651174301 \ No newline at end of file diff --git a/Editor/Drawers/UnityReferenceDrawer.cs b/Editor/Drawers/UnityReferenceDrawer.cs new file mode 100644 index 0000000..dbae677 --- /dev/null +++ b/Editor/Drawers/UnityReferenceDrawer.cs @@ -0,0 +1,36 @@ +using System; +using UnityEditor; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace TNRD.Drawers +{ + internal class UnityReferenceDrawer : ReferenceDrawer, IReferenceDrawer + { + private readonly GUIContent label; + + public UnityReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType) + : base(property, genericType) + { + this.label = label; + } + + public float GetHeight() + { + return EditorGUIUtility.singleLineHeight; + } + + public void OnGUI(Rect position) + { + Object unityReference = UnityReferenceProperty.objectReferenceValue; + Type referenceType = unityReference == null ? typeof(Object) : unityReference.GetType(); + GUIContent objectContent = EditorGUIUtility.ObjectContent(unityReference, referenceType); + CustomObjectDrawer.OnGUI(position, label, objectContent); + } + + protected override void OnPropertiesClicked() + { + PropertyEditorUtility.Show(UnityReferenceProperty.objectReferenceValue); + } + } +} \ No newline at end of file diff --git a/Editor/Drawers/UnityReferenceDrawer.cs.meta b/Editor/Drawers/UnityReferenceDrawer.cs.meta new file mode 100644 index 0000000..dd887ae --- /dev/null +++ b/Editor/Drawers/UnityReferenceDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 563fb2f2a8904b80bebd2da80d1a9a23 +timeCreated: 1651172014 \ No newline at end of file diff --git a/Editor/SerializableInterfacePropertyDrawer.Styles.cs.meta b/Editor/SerializableInterfacePropertyDrawer.Styles.cs.meta deleted file mode 100644 index 039b097..0000000 --- a/Editor/SerializableInterfacePropertyDrawer.Styles.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: cefe53c478ea4116b9e68507d94d2028 -timeCreated: 1651061584 \ No newline at end of file diff --git a/Editor/SerializableInterfacePropertyDrawer.cs b/Editor/SerializableInterfacePropertyDrawer.cs index 4dc7efb..add823d 100644 --- a/Editor/SerializableInterfacePropertyDrawer.cs +++ b/Editor/SerializableInterfacePropertyDrawer.cs @@ -1,43 +1,18 @@ using System; -using System.Collections.Generic; -using System.Linq; +using TNRD.Drawers; using UnityEditor; -using UnityEditor.IMGUI.Controls; using UnityEngine; using UnityEngine.Assertions; -using UnityEngine.SceneManagement; -using Object = UnityEngine.Object; namespace TNRD { [CustomPropertyDrawer(typeof(SerializableInterface<>), true)] - internal sealed partial class SerializableInterfacePropertyDrawer : PropertyDrawer + internal sealed class SerializableInterfacePropertyDrawer : PropertyDrawer { private SerializedProperty serializedProperty; - private bool labelSelected; - private bool objectPickerSelected; + private Type genericType; - private bool IsSelected => labelSelected || objectPickerSelected; - - private SerializedProperty RawReferenceProperty => serializedProperty.FindPropertyRelative("rawReference"); - private SerializedProperty UnityReferenceProperty => serializedProperty.FindPropertyRelative("unityReference"); - private SerializedProperty ReferenceModeProperty => serializedProperty.FindPropertyRelative("mode"); - - private ReferenceMode ReferenceMode => (ReferenceMode)ReferenceModeProperty.enumValueIndex; - - private object RawReferenceValue - { - get - { -#if UNITY_2021_1_OR_NEWER - return RawReferenceProperty.managedReferenceValue; -#else - ISerializableInterface instance = - (ISerializableInterface)fieldInfo.GetValue(serializedProperty.serializedObject.targetObject); - return instance.GetRawReference(); -#endif - } - } + private IReferenceDrawer activeDrawer; /// public override bool CanCacheInspectorGUI(SerializedProperty property) @@ -45,43 +20,30 @@ public override bool CanCacheInspectorGUI(SerializedProperty property) return false; } + private void Initialize(SerializedProperty property) + { + if (serializedProperty != null) + return; + + serializedProperty = property; + genericType = GetGenericArgument(); + Assert.IsNotNull(genericType, "Unable to find generic argument, are you doing some shady inheritance?"); + } + /// public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { - serializedProperty = property; - switch (ReferenceMode) - { - case ReferenceMode.Raw: - return EditorGUIUtility.singleLineHeight + - EditorGUIUtility.standardVerticalSpacing + - EditorGUI.GetPropertyHeight(RawReferenceProperty, true); - case ReferenceMode.Unity: - return EditorGUIUtility.singleLineHeight; - default: - throw new ArgumentOutOfRangeException(); - } + Initialize(property); + activeDrawer = GetReferenceDrawer(activeDrawer, property, label); + return activeDrawer.GetHeight(); } /// public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { - serializedProperty = property; - Type genericArgument = GetGenericArgument(); - Assert.IsNotNull(genericArgument, "Unable to find generic argument, are you doing some shady inheritance?"); - - switch (ReferenceMode) - { - case ReferenceMode.Raw: - DrawRawReferenceMode(position, property, label, genericArgument); - break; - case ReferenceMode.Unity: - DrawUnityReferenceMode(position, property, label, genericArgument); - break; - default: - throw new ArgumentOutOfRangeException(); - } - - HandleDeleteButton(); + Initialize(property); + activeDrawer = GetReferenceDrawer(activeDrawer, property, label); + activeDrawer.OnGUI(position); } private Type GetGenericArgument() @@ -108,231 +70,28 @@ private Type GetGenericArgument() return null; } - private void DrawRawReferenceMode( - Rect position, + private IReferenceDrawer GetReferenceDrawer( + IReferenceDrawer original, SerializedProperty property, - GUIContent label, - Type genericArgument + GUIContent label ) { - Rect objectFieldRect = new Rect(position) - { - height = EditorGUIUtility.singleLineHeight - }; + SerializedProperty modeProperty = serializedProperty.FindPropertyRelative("mode"); + ReferenceMode referenceMode = (ReferenceMode)modeProperty.enumValueIndex; - objectFieldRect = DrawPrefixLabel(objectFieldRect, label); - DrawRawReference(objectFieldRect); - DrawButton(objectFieldRect, property, genericArgument); - - Rect objectDrawerRect = new Rect(position); - objectDrawerRect.yMin += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; - - EditorGUI.PropertyField(objectDrawerRect, - RawReferenceProperty, - new GUIContent(RawReferenceValue.GetType().Name), - true); - } - - private void DrawRawReference(Rect position) - { - Type type = RawReferenceValue.GetType(); - string typeName = type.Name; - IEnumerable scripts = AssetDatabase.FindAssets($"t:Script {typeName}") - .Select(AssetDatabase.GUIDToAssetPath) - .Select(AssetDatabase.LoadAssetAtPath); - MonoScript monoScript = scripts.FirstOrDefault(x => x.GetClass() == type); - - DrawObjectField(position, new GUIContent(typeName, IconUtility.ScriptIcon), monoScript); - } - - private void DrawUnityReferenceMode( - Rect position, - SerializedProperty property, - GUIContent label, - Type genericArgument - ) - { - position = DrawPrefixLabel(position, label); - DrawUnityReference(position); - DrawButton(position, property, genericArgument); - } - - - private void DrawUnityReference(Rect position) - { - Object unityReference = UnityReferenceProperty.objectReferenceValue; - Type referenceType = unityReference == null ? typeof(Object) : unityReference.GetType(); - DrawObjectField(position, EditorGUIUtility.ObjectContent(unityReference, referenceType), unityReference); - } - - private Rect DrawPrefixLabel(Rect position, GUIContent label) - { - GUIStyle labelStyle = IsSelected ? Styles.SelectedLabelStyle : Styles.RegularLabelStyle; - Rect result = EditorGUI.PrefixLabel(position, label, labelStyle); - - if (Event.current.type == EventType.MouseDown) - { - Rect delta = new Rect(position) - { - width = position.width - result.width - }; - - labelSelected = delta.Contains(Event.current.mousePosition); - RepaintActiveInspector(); - } - - return result; - } - - private void DrawObjectField(Rect position, GUIContent objectFieldContent, Object objectToShow) - { - Rect positionWithoutThumb = new Rect(position); - positionWithoutThumb.xMax -= 20; - - Event evt = Event.current; - if (evt.type == EventType.Repaint) - { - EditorStyles.objectField.Draw(position, - objectFieldContent, - position.Contains(evt.mousePosition), - false, - false, - IsSelected); - } - - HandleObjectFieldMouseDown(position, objectToShow, evt, positionWithoutThumb); - } - - private void HandleObjectFieldMouseDown( - Rect position, - Object objectToShow, - Event evt, - Rect positionWithoutThumb - ) - { - if (evt.type != EventType.MouseDown) - return; - - if (evt.button == 0) - { - objectPickerSelected = positionWithoutThumb.Contains(evt.mousePosition); - PingObject(); - RepaintActiveInspector(); - } - else if (evt.button == 1 && positionWithoutThumb.Contains(evt.mousePosition)) - { - if (objectToShow != null) - { - GenericMenu menu = new GenericMenu(); - menu.AddItem(new GUIContent("Properties..."), - false, - () => { PropertyEditorUtility.Show(objectToShow); }); - menu.DropDown(position); - } - - evt.Use(); - } - } - - private void PingObject() - { - if (!IsSelected) - return; - - switch (ReferenceMode) + switch (referenceMode) { case ReferenceMode.Raw: - // No support for pinging raw objects for now (I guess this would ping the MonoScript?) - break; + return original is RawReferenceDrawer + ? original + : new RawReferenceDrawer(property, label, genericType, fieldInfo); case ReferenceMode.Unity: - EditorGUIUtility.PingObject(UnityReferenceProperty.objectReferenceValue); - break; + return original is UnityReferenceDrawer + ? original + : new UnityReferenceDrawer(property, label, genericType); default: throw new ArgumentOutOfRangeException(); } } - - private void RepaintActiveInspector() - { - // Forcing a repaint of the inspector for this object, not the prettiest but it works - foreach (Editor activeEditor in ActiveEditorTracker.sharedTracker.activeEditors) - { - if (activeEditor.serializedObject == serializedProperty.serializedObject) - { - activeEditor.Repaint(); - } - } - } - - private void DrawButton(Rect position, SerializedProperty property, Type genericArgument) - { - Rect buttonRect = new Rect(position); - buttonRect.yMin += 1; - buttonRect.yMax -= 1; - buttonRect.xMin = buttonRect.xMax - 20; - buttonRect.xMax -= 1; - - if (GUI.Button(buttonRect, string.Empty, "objectFieldButton")) - { - AdvancedDropdownState state = new AdvancedDropdownState(); - SerializableInterfaceAdvancedDropdown dropdown = - new SerializableInterfaceAdvancedDropdown(state, genericArgument, GetRelevantScene()); - dropdown.ItemSelectedEvent += (mode, reference) => - { - DropdownOnItemSelectedEvent(property, mode, reference); - }; - dropdown.Show(position); - } - } - - private Scene? GetRelevantScene() - { - Object target = serializedProperty.serializedObject.targetObject; - - if (target is ScriptableObject) - return null; - if (target is Component component) - return component.gameObject.scene; - if (target is GameObject gameObject) - return gameObject.scene; - - return null; - } - - private void DropdownOnItemSelectedEvent(SerializedProperty property, 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(); - } - - private void HandleDeleteButton() - { - if (!IsSelected) - return; - - Event evt = Event.current; - if (evt.type != EventType.KeyDown || evt.keyCode != KeyCode.Delete) - return; - - // Setting this to Unity because we don't handle null cases for Raw mode - ReferenceModeProperty.enumValueIndex = (int)ReferenceMode.Unity; - RawReferenceProperty.managedReferenceValue = null; - UnityReferenceProperty.objectReferenceValue = null; - - serializedProperty.serializedObject.ApplyModifiedProperties(); - } } } \ No newline at end of file