Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions Editor/Drawers/RawReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <inheritdoc />
Expand Down Expand Up @@ -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?)
}

/// <inheritdoc />
protected override void OnPropertiesClicked()
{
Expand Down
115 changes: 75 additions & 40 deletions Editor/Drawers/ReferenceDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using TNRD.Utilities;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -166,7 +204,7 @@ private void HandleDragUpdated()
SetDragAndDropMode(false);
return;
}

if (!GenericType.IsAssignableFrom(scriptType))
{
SetDragAndDropMode(false);
Expand All @@ -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();
}
}
10 changes: 8 additions & 2 deletions Editor/Drawers/UnityReferenceDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;
using TNRD.Utilities;
using UnityEditor;
using UnityEngine;
Expand All @@ -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;
}

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Editor/SerializableInterfacePropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down