Skip to content

Commit ae0f89f

Browse files
Moved changes to RawReferenceDrawer
Since it only applies to SerializableInterfaces in Raw mode, it made more sense to only do it in RawReferenceDrawer instead of SerializableInterfacePropertyDrawer. Tested the same way, no apparent diff.
1 parent d6e8d0b commit ae0f89f

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

Editor/Drawers/RawReferenceDrawer.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer
1111
private readonly GUIContent label;
1212
private readonly FieldInfo fieldInfo;
1313

14+
private static object previousReferenceValue;
15+
private static string previousPropertyPath;
16+
1417
private object RawReferenceValue
1518
{
1619
get
@@ -21,6 +24,15 @@ private object RawReferenceValue
2124
ISerializableInterface instance =
2225
(ISerializableInterface)fieldInfo.GetValue(Property.serializedObject.targetObject);
2326
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);
2436
#endif
2537
}
2638
}
@@ -47,6 +59,8 @@ public float GetHeight()
4759
/// <inheritdoc />
4860
public void OnGUI(Rect position)
4961
{
62+
AvoidDuplicateReferencesInArray();
63+
5064
Rect objectFieldRect = new Rect(position)
5165
{
5266
height = EditorGUIUtility.singleLineHeight
@@ -71,6 +85,8 @@ public void OnGUI(Rect position)
7185
true);
7286

7387
HandleDragAndDrop(objectFieldRect);
88+
89+
previousPropertyPath = Property.propertyPath;
7490
}
7591

7692
/// <inheritdoc />
@@ -91,5 +107,37 @@ protected override void OnPropertiesClicked()
91107
}
92108
}
93109
}
110+
111+
private void AvoidDuplicateReferencesInArray()
112+
{
113+
if (!IsPropertyInArray(Property)) return;
114+
if (previousPropertyPath == null) return;
115+
if (previousPropertyPath == Property.propertyPath) return;
116+
117+
var rawReferenceProperty = Property.FindPropertyRelative("rawReference");
118+
var currentReferenceValue = RawReferenceValue;
119+
120+
if (currentReferenceValue == null) return;
121+
122+
if (previousReferenceValue == currentReferenceValue)
123+
{
124+
rawReferenceProperty.managedReferenceValue = CreateInstance(currentReferenceValue);
125+
rawReferenceProperty.serializedObject.ApplyModifiedProperties();
126+
}
127+
128+
previousReferenceValue = currentReferenceValue;
129+
}
130+
131+
private static bool IsPropertyInArray(SerializedProperty prop)
132+
{
133+
return prop.propertyPath.Contains(".Array.data[");
134+
}
135+
136+
private static object CreateInstance(object source)
137+
{
138+
var instance = Activator.CreateInstance(source.GetType());
139+
EditorUtility.CopySerializedManagedFieldsOnly(source, instance);
140+
return instance;
141+
}
94142
}
95-
}
143+
}

Editor/SerializableInterfacePropertyDrawer.cs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@ namespace TNRD
1010
[CustomPropertyDrawer(typeof(SerializableInterface<>), true)]
1111
internal sealed class SerializableInterfacePropertyDrawer : PropertyDrawer
1212
{
13-
private const string RAW_REFERENCE = "rawReference";
14-
private const string MODE = "mode";
15-
1613
private SerializedProperty serializedProperty;
1714
private Type genericType;
1815

1916
private IReferenceDrawer activeDrawer;
2017

21-
private object previousReferenceValue;
22-
private string previousPropertyPath;
23-
2418
/// <inheritdoc />
2519
public override bool CanCacheInspectorGUI(SerializedProperty property) => false;
2620

@@ -48,48 +42,8 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
4842
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
4943
{
5044
Initialize(property);
51-
AvoidDuplicateReferencesInArray(property);
52-
5345
activeDrawer = GetReferenceDrawer(activeDrawer, property, label);
5446
activeDrawer.OnGUI(position);
55-
previousPropertyPath = property.propertyPath;
56-
}
57-
58-
/// <summary>
59-
/// When a new instance of <see cref="SerializableInterface{T}"/> is added in an array using the inspector's gizmo,
60-
/// avoids having their <see cref="SerializableInterface{T}.rawReference"/> field reference the same instance.
61-
/// </summary>
62-
/// <param name="property">The SerializedProperty to make the custom GUI for.</param>
63-
private void AvoidDuplicateReferencesInArray(SerializedProperty property)
64-
{
65-
if (!IsPropertyInArray(property)) return;
66-
if (previousPropertyPath == null) return;
67-
if (previousPropertyPath == property.propertyPath) return;
68-
69-
var rawReferenceProperty = property.FindPropertyRelative(RAW_REFERENCE);
70-
var currentReferenceValue = rawReferenceProperty.managedReferenceValue;
71-
72-
if (currentReferenceValue == null) return;
73-
74-
if (previousReferenceValue == currentReferenceValue)
75-
{
76-
rawReferenceProperty.managedReferenceValue = CreateInstance(currentReferenceValue);
77-
rawReferenceProperty.serializedObject.ApplyModifiedProperties();
78-
}
79-
80-
previousReferenceValue = currentReferenceValue;
81-
}
82-
83-
private static bool IsPropertyInArray(SerializedProperty prop)
84-
{
85-
return prop.propertyPath.Contains(".Array.data[");
86-
}
87-
88-
private static object CreateInstance(object source)
89-
{
90-
var instance = Activator.CreateInstance(source.GetType());
91-
EditorUtility.CopySerializedManagedFieldsOnly(source, instance);
92-
return instance;
9347
}
9448

9549
private Type GetGenericArgument()
@@ -134,7 +88,7 @@ private IReferenceDrawer GetReferenceDrawer(
13488
GUIContent label
13589
)
13690
{
137-
SerializedProperty modeProperty = serializedProperty.FindPropertyRelative(MODE);
91+
SerializedProperty modeProperty = serializedProperty.FindPropertyRelative("mode");
13892
ReferenceMode referenceMode = (ReferenceMode)modeProperty.enumValueIndex;
13993

14094
switch (referenceMode)

0 commit comments

Comments
 (0)