@@ -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,41 @@ protected override void OnPropertiesClicked()
91107 }
92108 }
93109 }
110+
111+ private void AvoidDuplicateReferencesInArray ( )
112+ {
113+ if ( ! IsPropertyInArray ( Property ) )
114+ return ;
115+ if ( previousPropertyPath == null )
116+ return ;
117+ if ( previousPropertyPath == Property . propertyPath )
118+ return ;
119+
120+ SerializedProperty rawReferenceProperty = Property . FindPropertyRelative ( "rawReference" ) ;
121+ object currentReferenceValue = RawReferenceValue ;
122+
123+ if ( currentReferenceValue == null )
124+ return ;
125+
126+ if ( previousReferenceValue == currentReferenceValue )
127+ {
128+ RawReferenceValue = CreateInstance ( currentReferenceValue ) ;
129+ rawReferenceProperty . serializedObject . ApplyModifiedProperties ( ) ;
130+ }
131+
132+ previousReferenceValue = currentReferenceValue ;
133+ }
134+
135+ private static bool IsPropertyInArray ( SerializedProperty prop )
136+ {
137+ return prop . propertyPath . Contains ( ".Array.data[" ) ;
138+ }
139+
140+ private static object CreateInstance ( object source )
141+ {
142+ object instance = Activator . CreateInstance ( source . GetType ( ) ) ;
143+ EditorUtility . CopySerializedManagedFieldsOnly ( source , instance ) ;
144+ return instance ;
145+ }
94146 }
95147}
0 commit comments