Skip to content

Commit 29411d2

Browse files
authored
Merge pull request #27 from marc-antoine-girard/#25-Fix-Setter-ReferenceDrawer
2 parents 05908da + 5a14905 commit 29411d2

File tree

4 files changed

+90
-68
lines changed

4 files changed

+90
-68
lines changed

Editor/Drawers/RawReferenceDrawer.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,10 @@ internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer
1414
private object previousReferenceValue;
1515
private string previousPropertyPath;
1616

17-
private object RawReferenceValue
18-
{
19-
get
20-
{
21-
#if UNITY_2021_1_OR_NEWER
22-
return RawReferenceProperty.managedReferenceValue;
23-
#else
24-
ISerializableInterface instance =
25-
(ISerializableInterface)fieldInfo.GetValue(Property.serializedObject.targetObject);
26-
return instance.GetRawReference();
27-
#endif
28-
}
29-
30-
set
31-
{
32-
#if UNITY_2021_1_OR_NEWER
33-
RawReferenceProperty.managedReferenceValue = value;
34-
#else
35-
fieldInfo.SetValue(Property.serializedObject.targetObject, value);
36-
#endif
37-
}
38-
}
39-
4017
public void Initialize(SerializedProperty property, Type genericType, GUIContent label, FieldInfo fieldInfo)
4118
{
42-
Initialize(property, genericType);
19+
Initialize(property, genericType, fieldInfo);
4320
this.label = label;
44-
this.fieldInfo = fieldInfo;
4521
}
4622

4723
/// <inheritdoc />
@@ -89,6 +65,11 @@ public void OnGUI(Rect position)
8965
previousPropertyPath = Property.propertyPath;
9066
}
9167

68+
protected override void PingObject()
69+
{
70+
// No support for pinging raw objects for now (I guess this would ping the MonoScript?)
71+
}
72+
9273
/// <inheritdoc />
9374
protected override void OnPropertiesClicked()
9475
{

Editor/Drawers/ReferenceDrawer.cs

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Reflection;
34
using TNRD.Utilities;
45
using UnityEditor;
56
using UnityEditor.IMGUI.Controls;
@@ -30,6 +31,67 @@ private enum DragAndDropMode
3031
protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference");
3132
protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference");
3233

34+
protected FieldInfo FieldInfo { get; private set; }
35+
36+
protected ReferenceMode ModeValue
37+
{
38+
get => (ReferenceMode)ReferenceModeProperty.enumValueIndex;
39+
set => ReferenceModeProperty.enumValueIndex = (int)value;
40+
}
41+
42+
protected object RawReferenceValue
43+
{
44+
get
45+
{
46+
#if UNITY_2021_1_OR_NEWER
47+
return RawReferenceProperty.managedReferenceValue;
48+
#else
49+
ISerializableInterface instance =
50+
(ISerializableInterface)FieldInfo.GetValue(Property.serializedObject.targetObject);
51+
return instance.GetRawReference();
52+
#endif
53+
}
54+
set
55+
{
56+
#if UNITY_2021_1_OR_NEWER
57+
RawReferenceProperty.managedReferenceValue = value;
58+
#else
59+
FieldInfo.SetValue(Property.serializedObject.targetObject, value);
60+
#endif
61+
}
62+
}
63+
64+
protected object PropertyValue
65+
{
66+
get
67+
{
68+
return ModeValue switch
69+
{
70+
ReferenceMode.Raw => RawReferenceValue,
71+
ReferenceMode.Unity => UnityReferenceProperty.objectReferenceValue,
72+
_ => throw new ArgumentOutOfRangeException()
73+
};
74+
}
75+
set
76+
{
77+
switch (ModeValue)
78+
{
79+
case ReferenceMode.Raw:
80+
RawReferenceValue = value;
81+
UnityReferenceProperty.objectReferenceValue = null;
82+
break;
83+
case ReferenceMode.Unity:
84+
UnityReferenceProperty.objectReferenceValue = (Object)value;
85+
RawReferenceValue = null;
86+
break;
87+
default:
88+
throw new ArgumentOutOfRangeException();
89+
}
90+
91+
Property.serializedObject.ApplyModifiedProperties();
92+
}
93+
}
94+
3395
protected ReferenceDrawer()
3496
{
3597
CustomObjectDrawer = new CustomObjectDrawer();
@@ -39,10 +101,11 @@ protected ReferenceDrawer()
39101
CustomObjectDrawer.PropertiesClicked += OnPropertiesClicked;
40102
}
41103

42-
protected void Initialize(SerializedProperty property, Type genericType)
104+
protected void Initialize(SerializedProperty property, Type genericType, FieldInfo fieldInfo)
43105
{
44106
Property = property;
45107
GenericType = genericType;
108+
FieldInfo = fieldInfo;
46109
}
47110

48111
private void OnButtonClicked(Rect position)
@@ -70,43 +133,18 @@ private void OnButtonClicked(Rect position)
70133

71134
private void OnClicked()
72135
{
73-
switch ((ReferenceMode)ReferenceModeProperty.enumValueIndex)
74-
{
75-
case ReferenceMode.Raw:
76-
// No support for pinging raw objects for now (I guess this would ping the MonoScript?)
77-
break;
78-
case ReferenceMode.Unity:
79-
EditorGUIUtility.PingObject(UnityReferenceProperty.objectReferenceValue);
80-
break;
81-
default:
82-
throw new ArgumentOutOfRangeException();
83-
}
136+
PingObject();
84137
}
85138

86139
private void OnDeletePressed()
87140
{
88-
RawReferenceProperty.managedReferenceValue = null;
89-
UnityReferenceProperty.objectReferenceValue = null;
90-
Property.serializedObject.ApplyModifiedProperties();
141+
PropertyValue = null;
91142
}
92143

93144
private void OnItemSelected(ReferenceMode mode, object reference)
94145
{
95-
ReferenceModeProperty.enumValueIndex = (int)mode;
96-
97-
switch (mode)
98-
{
99-
case ReferenceMode.Raw:
100-
RawReferenceProperty.managedReferenceValue = reference;
101-
break;
102-
case ReferenceMode.Unity:
103-
UnityReferenceProperty.objectReferenceValue = (Object)reference;
104-
break;
105-
default:
106-
throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
107-
}
108-
109-
Property.serializedObject.ApplyModifiedProperties();
146+
ModeValue = mode;
147+
PropertyValue = reference;
110148
}
111149

112150
protected abstract void OnPropertiesClicked();
@@ -166,7 +204,7 @@ private void HandleDragUpdated()
166204
SetDragAndDropMode(false);
167205
return;
168206
}
169-
207+
170208
if (!GenericType.IsAssignableFrom(scriptType))
171209
{
172210
SetDragAndDropMode(false);
@@ -187,22 +225,19 @@ private void HandleDragPerform()
187225
switch (dragAndDropMode)
188226
{
189227
case DragAndDropMode.Raw:
190-
RawReferenceProperty.managedReferenceValue =
191-
Activator.CreateInstance(((MonoScript)DragAndDrop.objectReferences[0]).GetClass());
192-
ReferenceModeProperty.enumValueIndex = (int)ReferenceMode.Raw;
228+
ModeValue = ReferenceMode.Raw;
229+
PropertyValue = Activator.CreateInstance(((MonoScript)DragAndDrop.objectReferences[0]).GetClass());
193230
break;
194231
case DragAndDropMode.Unity:
195-
if(DragAndDrop.objectReferences[0] is GameObject go)
196-
UnityReferenceProperty.objectReferenceValue = go.GetComponent(GenericType);
197-
else
198-
UnityReferenceProperty.objectReferenceValue = DragAndDrop.objectReferences[0];
199-
200-
ReferenceModeProperty.enumValueIndex = (int)ReferenceMode.Unity;
232+
ModeValue = ReferenceMode.Unity;
233+
PropertyValue = DragAndDrop.objectReferences[0];
201234
break;
202235
case DragAndDropMode.None:
203236
default:
204237
throw new ArgumentOutOfRangeException();
205238
}
206239
}
240+
241+
protected abstract void PingObject();
207242
}
208243
}

Editor/Drawers/UnityReferenceDrawer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using TNRD.Utilities;
34
using UnityEditor;
45
using UnityEngine;
@@ -10,9 +11,9 @@ internal class UnityReferenceDrawer : ReferenceDrawer, IReferenceDrawer
1011
{
1112
private GUIContent label;
1213

13-
public void Initialize(SerializedProperty property, Type genericType, GUIContent label)
14+
public void Initialize(SerializedProperty property, Type genericType, GUIContent label, FieldInfo fieldInfo)
1415
{
15-
Initialize(property, genericType);
16+
Initialize(property, genericType, fieldInfo);
1617
this.label = label;
1718
}
1819

@@ -30,6 +31,11 @@ public void OnGUI(Rect position)
3031
HandleDragAndDrop(position);
3132
}
3233

34+
protected override void PingObject()
35+
{
36+
EditorGUIUtility.PingObject((Object)PropertyValue);
37+
}
38+
3339
protected override void OnPropertiesClicked()
3440
{
3541
PropertyEditorUtility.Show(UnityReferenceProperty.objectReferenceValue);

Editor/SerializableInterfacePropertyDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private IReferenceDrawer GetReferenceDrawer(SerializedProperty property, GUICont
9090
rawReferenceDrawer.Initialize(property, genericType, label, fieldInfo);
9191
return rawReferenceDrawer;
9292
case ReferenceMode.Unity:
93-
unityReferenceDrawer.Initialize(property, genericType, label);
93+
unityReferenceDrawer.Initialize(property, genericType, label, fieldInfo);
9494
return unityReferenceDrawer;
9595
default:
9696
throw new ArgumentOutOfRangeException();

0 commit comments

Comments
 (0)