Skip to content

Commit 4f4e894

Browse files
authored
Merge pull request #36 from Thundernerd/feature/caching_reference_drawers
2 parents 9ddae3a + f930d8e commit 4f4e894

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

Editor/Drawers/RawReferenceDrawer.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ namespace TNRD.Drawers
88
{
99
internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer
1010
{
11-
private readonly GUIContent label;
12-
private readonly FieldInfo fieldInfo;
11+
private GUIContent label;
12+
private FieldInfo fieldInfo;
1313

14-
private static object previousReferenceValue;
15-
private static string previousPropertyPath;
14+
private object previousReferenceValue;
15+
private string previousPropertyPath;
1616

1717
private object RawReferenceValue
1818
{
@@ -37,10 +37,9 @@ private object RawReferenceValue
3737
}
3838
}
3939

40-
/// <inheritdoc />
41-
public RawReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType, FieldInfo fieldInfo)
42-
: base(property, genericType)
40+
public void Initialize(SerializedProperty property, Type genericType, GUIContent label, FieldInfo fieldInfo)
4341
{
42+
Initialize(property, genericType);
4443
this.label = label;
4544
this.fieldInfo = fieldInfo;
4645
}

Editor/Drawers/ReferenceDrawer.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,30 @@ private enum DragAndDropMode
2121

2222
private DragAndDropMode dragAndDropMode;
2323

24-
protected readonly SerializedProperty Property;
25-
protected readonly Type GenericType;
2624
protected readonly CustomObjectDrawer CustomObjectDrawer;
2725

26+
protected SerializedProperty Property { get; private set; }
27+
protected Type GenericType { get; private set; }
28+
2829
protected SerializedProperty ReferenceModeProperty => Property.FindPropertyRelative("mode");
2930
protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference");
3031
protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference");
3132

32-
protected ReferenceDrawer(SerializedProperty property, Type genericType)
33+
protected ReferenceDrawer()
3334
{
34-
Property = property;
35-
GenericType = genericType;
36-
3735
CustomObjectDrawer = new CustomObjectDrawer();
3836
CustomObjectDrawer.ButtonClicked += OnButtonClicked;
3937
CustomObjectDrawer.Clicked += OnClicked;
4038
CustomObjectDrawer.DeletePressed += OnDeletePressed;
4139
CustomObjectDrawer.PropertiesClicked += OnPropertiesClicked;
4240
}
4341

42+
protected void Initialize(SerializedProperty property, Type genericType)
43+
{
44+
Property = property;
45+
GenericType = genericType;
46+
}
47+
4448
private void OnButtonClicked(Rect position)
4549
{
4650
AdvancedDropdownState state = new AdvancedDropdownState();

Editor/Drawers/UnityReferenceDrawer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ namespace TNRD.Drawers
88
{
99
internal class UnityReferenceDrawer : ReferenceDrawer, IReferenceDrawer
1010
{
11-
private readonly GUIContent label;
11+
private GUIContent label;
1212

13-
public UnityReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType)
14-
: base(property, genericType)
13+
public void Initialize(SerializedProperty property, Type genericType, GUIContent label)
1514
{
15+
Initialize(property, genericType);
1616
this.label = label;
1717
}
1818

Editor/SerializableInterfacePropertyDrawer.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ namespace TNRD
1010
[CustomPropertyDrawer(typeof(SerializableInterface<>), true)]
1111
internal sealed class SerializableInterfacePropertyDrawer : PropertyDrawer
1212
{
13+
private readonly RawReferenceDrawer rawReferenceDrawer = new RawReferenceDrawer();
14+
private readonly UnityReferenceDrawer unityReferenceDrawer = new UnityReferenceDrawer();
15+
1316
private SerializedProperty serializedProperty;
1417
private Type genericType;
1518

16-
private IReferenceDrawer activeDrawer;
17-
1819
/// <inheritdoc />
1920
public override bool CanCacheInspectorGUI(SerializedProperty property) => false;
2021

@@ -23,7 +24,6 @@ private void Initialize(SerializedProperty property)
2324
if (serializedProperty == property)
2425
return;
2526

26-
activeDrawer = null;
2727
serializedProperty = property;
2828
genericType = GetGenericArgument();
2929
Assert.IsNotNull(genericType, "Unable to find generic argument, are you doing some shady inheritance?");
@@ -33,16 +33,14 @@ private void Initialize(SerializedProperty property)
3333
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
3434
{
3535
Initialize(property);
36-
activeDrawer = GetReferenceDrawer(activeDrawer, property, label);
37-
return activeDrawer.GetHeight();
36+
return GetReferenceDrawer(property, label).GetHeight();
3837
}
3938

4039
/// <inheritdoc />
4140
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
4241
{
4342
Initialize(property);
44-
activeDrawer = GetReferenceDrawer(activeDrawer, property, label);
45-
activeDrawer.OnGUI(position);
43+
GetReferenceDrawer(property, label).OnGUI(position);
4644
}
4745

4846
private Type GetGenericArgument()
@@ -81,25 +79,19 @@ private Type GetGenericArgument()
8179
return null;
8280
}
8381

84-
private IReferenceDrawer GetReferenceDrawer(
85-
IReferenceDrawer original,
86-
SerializedProperty property,
87-
GUIContent label
88-
)
82+
private IReferenceDrawer GetReferenceDrawer(SerializedProperty property, GUIContent label)
8983
{
9084
SerializedProperty modeProperty = serializedProperty.FindPropertyRelative("mode");
9185
ReferenceMode referenceMode = (ReferenceMode)modeProperty.enumValueIndex;
9286

9387
switch (referenceMode)
9488
{
9589
case ReferenceMode.Raw:
96-
return original is RawReferenceDrawer
97-
? original
98-
: new RawReferenceDrawer(property, label, genericType, fieldInfo);
90+
rawReferenceDrawer.Initialize(property, genericType, label, fieldInfo);
91+
return rawReferenceDrawer;
9992
case ReferenceMode.Unity:
100-
return original is UnityReferenceDrawer
101-
? original
102-
: new UnityReferenceDrawer(property, label, genericType);
93+
unityReferenceDrawer.Initialize(property, genericType, label);
94+
return unityReferenceDrawer;
10395
default:
10496
throw new ArgumentOutOfRangeException();
10597
}

0 commit comments

Comments
 (0)