Skip to content

Commit b732ae2

Browse files
authored
Merge pull request #44 from Thundernerd/fix/caching_drawers
2 parents 6af383e + 5913ab7 commit b732ae2

File tree

7 files changed

+153
-96
lines changed

7 files changed

+153
-96
lines changed

Editor/Drawers/CustomObjectDrawer.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace TNRD.Drawers
55
{
66
public partial class CustomObjectDrawer
77
{
8-
public delegate void ButtonClickedDelegate(Rect position);
8+
public delegate void ButtonClickedDelegate(Rect position, SerializedProperty property);
99

10-
public delegate void ClickedDelegate();
10+
public delegate void ClickedDelegate(SerializedProperty property);
1111

12-
public delegate void DeletePressedDelegate();
12+
public delegate void DeletePressedDelegate(SerializedProperty property);
1313

14-
public delegate void PropertiesClickedDelegate();
14+
public delegate void PropertiesClickedDelegate(SerializedProperty property);
1515

1616
private bool isSelected;
1717

@@ -21,18 +21,18 @@ public partial class CustomObjectDrawer
2121
public event ClickedDelegate Clicked;
2222
public event DeletePressedDelegate DeletePressed;
2323
public event PropertiesClickedDelegate PropertiesClicked;
24-
25-
public void OnGUI(Rect position, GUIContent label, GUIContent content)
24+
25+
public void OnGUI(Rect position, GUIContent label, GUIContent content, SerializedProperty property)
2626
{
2727
Rect positionWithoutThumb = new Rect(position);
2828
positionWithoutThumb.xMax -= 20;
2929

3030
position = DrawPrefixLabel(position, label);
3131
DrawObjectField(position, content);
32-
DrawButton(position);
32+
DrawButton(position, property);
3333

34-
HandleMouseDown(position, positionWithoutThumb);
35-
HandleKeyDown();
34+
HandleMouseDown(position, positionWithoutThumb, property);
35+
HandleKeyDown(property);
3636
}
3737

3838
private Rect DrawPrefixLabel(Rect position, GUIContent label)
@@ -66,7 +66,7 @@ private void ForceRepaintEditors()
6666
}
6767
}
6868

69-
private void DrawButton(Rect position)
69+
private void DrawButton(Rect position, SerializedProperty property)
7070
{
7171
Rect buttonRect = new Rect(position);
7272
buttonRect.yMin += 1;
@@ -76,11 +76,11 @@ private void DrawButton(Rect position)
7676

7777
if (GUI.Button(buttonRect, string.Empty, "objectFieldButton"))
7878
{
79-
ButtonClicked?.Invoke(position);
79+
ButtonClicked?.Invoke(position, property);
8080
}
8181
}
8282

83-
private void HandleMouseDown(Rect position, Rect positionWithoutThumb)
83+
private void HandleMouseDown(Rect position, Rect positionWithoutThumb, SerializedProperty property)
8484
{
8585
if (Event.type != EventType.MouseDown)
8686
return;
@@ -89,26 +89,26 @@ private void HandleMouseDown(Rect position, Rect positionWithoutThumb)
8989
{
9090
isSelected = positionWithoutThumb.Contains(Event.mousePosition);
9191
ForceRepaintEditors();
92-
Clicked?.Invoke();
92+
Clicked?.Invoke(property);
9393
}
9494
else if (Event.button == 1 && positionWithoutThumb.Contains(Event.mousePosition))
9595
{
9696
GenericMenu menu = new GenericMenu();
97-
menu.AddItem(new GUIContent("Clear"), false, () => { DeletePressed?.Invoke(); });
98-
menu.AddItem(new GUIContent("Properties..."), false, () => { PropertiesClicked?.Invoke(); });
97+
menu.AddItem(new GUIContent("Clear"), false, () => { DeletePressed?.Invoke(property); });
98+
menu.AddItem(new GUIContent("Properties..."), false, () => { PropertiesClicked?.Invoke(property); });
9999
menu.DropDown(position);
100100
Event.Use();
101101
}
102102
}
103103

104-
private void HandleKeyDown()
104+
private void HandleKeyDown(SerializedProperty property)
105105
{
106106
if (!isSelected)
107107
return;
108108

109109
if (Event.type == EventType.KeyDown && Event.keyCode == KeyCode.Delete)
110110
{
111-
DeletePressed?.Invoke();
111+
DeletePressed?.Invoke(property);
112112
}
113113
}
114114
}

Editor/Drawers/RawReferenceDrawer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void OnGUI(Rect position)
4545
? EditorGUIUtility.ObjectContent((MonoScript)null, typeof(MonoScript))
4646
: new GUIContent(rawReferenceValue.GetType().Name, IconUtility.ScriptIcon);
4747

48-
CustomObjectDrawer.OnGUI(objectFieldRect, label, content);
48+
CustomObjectDrawer.OnGUI(objectFieldRect, label, content, Property);
4949

5050
HandleDragAndDrop(objectFieldRect);
5151

@@ -79,13 +79,13 @@ private void DrawLine(Rect position)
7979
EditorGUI.DrawRect(line, Styles.LineColor);
8080
}
8181

82-
protected override void PingObject()
82+
protected override void PingObject(SerializedProperty property)
8383
{
8484
// No support for pinging raw objects for now (I guess this would ping the MonoScript?)
8585
}
8686

8787
/// <inheritdoc />
88-
protected override void OnPropertiesClicked()
88+
protected override void OnPropertiesClicked(SerializedProperty property)
8989
{
9090
if (RawReferenceValue == null)
9191
return;

Editor/Drawers/ReferenceDrawer.cs

Lines changed: 94 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,69 +27,28 @@ private enum DragAndDropMode
2727
protected SerializedProperty Property { get; private set; }
2828
protected Type GenericType { get; private set; }
2929

30-
protected SerializedProperty ReferenceModeProperty => Property.FindPropertyRelative("mode");
31-
protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference");
32-
protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference");
30+
protected SerializedProperty ReferenceModeProperty => Property.ReferenceModeProperty();
31+
protected SerializedProperty RawReferenceProperty => Property.RawReferenceProperty();
32+
protected SerializedProperty UnityReferenceProperty => Property.UnityReferenceProperty();
3333

3434
protected FieldInfo FieldInfo { get; private set; }
3535

3636
protected ReferenceMode ModeValue
3737
{
38-
get => (ReferenceMode)ReferenceModeProperty.enumValueIndex;
39-
set => ReferenceModeProperty.enumValueIndex = (int)value;
38+
get => GetModeValue(Property);
39+
set => SetModeValue(Property, value);
4040
}
4141

4242
protected object RawReferenceValue
4343
{
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-
}
44+
get => GetRawReferenceValue(Property);
45+
set => SetRawReferenceValue(Property, value);
6246
}
6347

6448
protected object PropertyValue
6549
{
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 = GetUnityObject((Object)value);
85-
RawReferenceValue = null;
86-
break;
87-
default:
88-
throw new ArgumentOutOfRangeException();
89-
}
90-
91-
Property.serializedObject.ApplyModifiedProperties();
92-
}
50+
get => GetPropertyValue(Property);
51+
set => SetPropertyValue(Property, value);
9352
}
9453

9554
protected ReferenceDrawer()
@@ -108,18 +67,18 @@ protected void Initialize(SerializedProperty property, Type genericType, FieldIn
10867
FieldInfo = fieldInfo;
10968
}
11069

111-
private void OnButtonClicked(Rect position)
70+
private void OnButtonClicked(Rect position, SerializedProperty property)
11271
{
11372
AdvancedDropdownState state = new AdvancedDropdownState();
11473
SerializableInterfaceAdvancedDropdown dropdown =
115-
new SerializableInterfaceAdvancedDropdown(state, GenericType, GetRelevantScene());
74+
new SerializableInterfaceAdvancedDropdown(state, GenericType, GetRelevantScene(property), property);
11675
dropdown.ItemSelectedEvent += OnItemSelected;
11776
dropdown.Show(position);
11877
}
11978

120-
private Scene? GetRelevantScene()
79+
private static Scene? GetRelevantScene(SerializedProperty property)
12180
{
122-
Object target = Property.serializedObject.targetObject;
81+
Object target = property.serializedObject.targetObject;
12382

12483
if (target is ScriptableObject)
12584
return null;
@@ -131,30 +90,30 @@ private void OnButtonClicked(Rect position)
13190
return null;
13291
}
13392

134-
private void OnClicked()
93+
private void OnClicked(SerializedProperty property)
13594
{
136-
PingObject();
95+
PingObject(property);
13796
}
13897

139-
private void OnDeletePressed()
98+
private void OnDeletePressed(SerializedProperty property)
14099
{
141-
ModeValue = default;
142-
PropertyValue = null;
100+
SetModeValue(property, default);
101+
SetPropertyValue(property, null);
143102
}
144103

145-
private void OnItemSelected(ReferenceMode mode, object reference)
104+
private void OnItemSelected(SerializedProperty property, ReferenceMode mode, object reference)
146105
{
147-
ModeValue = mode;
148-
PropertyValue = reference;
106+
SetModeValue(property, mode);
107+
SetPropertyValue(property, reference);
149108
}
150109

151-
protected abstract void OnPropertiesClicked();
110+
protected abstract void OnPropertiesClicked(SerializedProperty property);
152111

153112
protected void HandleDragAndDrop(Rect position)
154113
{
155114
if (!position.Contains(Event.current.mousePosition))
156115
return;
157-
116+
158117
if (Event.current.type == EventType.DragPerform)
159118
{
160119
HandleDragUpdated();
@@ -241,11 +200,80 @@ private void HandleDragPerform()
241200

242201
private Object GetUnityObject(Object objectReference)
243202
{
244-
if(objectReference is GameObject gameObject)
203+
if (objectReference is GameObject gameObject)
245204
return gameObject.GetComponent(GenericType);
246205
return objectReference;
247206
}
248207

249-
protected abstract void PingObject();
208+
protected abstract void PingObject(SerializedProperty property);
209+
210+
protected ReferenceMode GetModeValue(SerializedProperty property)
211+
{
212+
return (ReferenceMode)property.ReferenceModeProperty().enumValueIndex;
213+
}
214+
215+
protected void SetModeValue(SerializedProperty property, ReferenceMode mode)
216+
{
217+
property.ReferenceModeProperty().enumValueIndex = (int)mode;
218+
}
219+
220+
protected object GetRawReferenceValue(SerializedProperty property)
221+
{
222+
#if UNITY_2021_1_OR_NEWER
223+
return property.RawReferenceProperty().managedReferenceValue;
224+
#else
225+
ISerializableInterface instance =
226+
(ISerializableInterface)FieldInfo.GetValue(property.serializedObject.targetObject);
227+
return instance.GetRawReference();
228+
#endif
229+
}
230+
231+
protected void SetRawReferenceValue(SerializedProperty property, object value)
232+
{
233+
#if UNITY_2021_1_OR_NEWER
234+
property.RawReferenceProperty().managedReferenceValue = value;
235+
#else
236+
FieldInfo.SetValue(property.serializedObject.targetObject, value);
237+
#endif
238+
}
239+
240+
protected Object GetUnityReferenceValue(SerializedProperty property)
241+
{
242+
return property.UnityReferenceProperty().objectReferenceValue;
243+
}
244+
245+
protected void SetUnityReferenceValue(SerializedProperty property, object value)
246+
{
247+
property.UnityReferenceProperty().objectReferenceValue = GetUnityObject((Object)value);
248+
}
249+
250+
protected object GetPropertyValue(SerializedProperty property)
251+
{
252+
return GetModeValue(property) switch
253+
{
254+
ReferenceMode.Raw => GetRawReferenceValue(property),
255+
ReferenceMode.Unity => GetUnityReferenceValue(property),
256+
_ => throw new ArgumentOutOfRangeException()
257+
};
258+
}
259+
260+
protected void SetPropertyValue(SerializedProperty property, object value)
261+
{
262+
switch (GetModeValue(property))
263+
{
264+
case ReferenceMode.Unity:
265+
SetUnityReferenceValue(property, value);
266+
SetRawReferenceValue(property, null);
267+
break;
268+
case ReferenceMode.Raw:
269+
SetRawReferenceValue(property, value);
270+
SetUnityReferenceValue(property, null);
271+
break;
272+
default:
273+
throw new ArgumentOutOfRangeException();
274+
}
275+
276+
property.serializedObject.ApplyModifiedProperties();
277+
}
250278
}
251279
}

Editor/Drawers/UnityReferenceDrawer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ public void OnGUI(Rect position)
2727
Object unityReference = UnityReferenceProperty.objectReferenceValue;
2828
Type referenceType = unityReference == null ? typeof(Object) : unityReference.GetType();
2929
GUIContent objectContent = EditorGUIUtility.ObjectContent(unityReference, referenceType);
30-
CustomObjectDrawer.OnGUI(position, label, objectContent);
30+
CustomObjectDrawer.OnGUI(position, label, objectContent, Property);
3131
HandleDragAndDrop(position);
3232
}
3333

34-
protected override void PingObject()
34+
protected override void PingObject(SerializedProperty property)
3535
{
36-
EditorGUIUtility.PingObject((Object)PropertyValue);
36+
EditorGUIUtility.PingObject((Object)GetPropertyValue(property));
3737
}
3838

39-
protected override void OnPropertiesClicked()
39+
protected override void OnPropertiesClicked(SerializedProperty property)
4040
{
41-
PropertyEditorUtility.Show(UnityReferenceProperty.objectReferenceValue);
41+
PropertyEditorUtility.Show(property.UnityReferenceProperty().objectReferenceValue);
4242
}
4343
}
4444
}

0 commit comments

Comments
 (0)