Skip to content

Commit aba0c30

Browse files
committed
feat: added methods for getting and setting values with serialized properties
1 parent 547c892 commit aba0c30

File tree

1 file changed

+78
-50
lines changed

1 file changed

+78
-50
lines changed

Editor/Drawers/ReferenceDrawer.cs

Lines changed: 78 additions & 50 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()
@@ -247,5 +206,74 @@ private Object GetUnityObject(Object objectReference)
247206
}
248207

249208
protected abstract void PingObject();
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
}

0 commit comments

Comments
 (0)