Skip to content

Commit f6a6c70

Browse files
authored
Merge pull request #169 from brunomikoski/feature/add-support-for-reflection-value-assign-for-generic
Added generic value support for generators
2 parents a4342d7 + 31319b0 commit f6a6c70

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

Scripts/Editor/Extensions/SerializedPropertyExtensions.cs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,55 @@ public static FieldInfo GetFieldInfoFromPathByType(this SerializedProperty prope
2828

2929
return fieldInfo;
3030
}
31-
32-
public static void SetValue(this SerializedProperty serializedProperty, object value)
31+
32+
public static void SetValueReflective(this SerializedProperty prop, object value)
3333
{
34+
object root = prop.serializedObject.targetObject;
35+
36+
string[] parts = prop.propertyPath
37+
.Replace(".Array.data[", "[")
38+
.Split('.');
39+
40+
object current = root;
41+
FieldInfo fi = null;
42+
43+
for (int i = 0; i < parts.Length; i++)
44+
{
45+
string part = parts[i];
46+
47+
if (part.EndsWith("]"))
48+
{
49+
int b = part.IndexOf('[');
50+
string name = part.Substring(0, b);
51+
int idx = int.Parse(part.Substring(b + 1, part.Length - b - 2));
52+
53+
fi = current.GetType()
54+
.GetField(name,
55+
BindingFlags.Instance
56+
| BindingFlags.NonPublic
57+
| BindingFlags.Public);
58+
IList list = fi.GetValue(current) as System.Collections.IList;
59+
current = list[idx];
60+
}
61+
else
62+
{
63+
fi = current.GetType()
64+
.GetField(part,
65+
BindingFlags.Instance
66+
| BindingFlags.NonPublic
67+
| BindingFlags.Public);
68+
if (i < parts.Length - 1)
69+
current = fi.GetValue(current);
70+
}
71+
}
72+
73+
fi.SetValue(current, value);
74+
75+
prop.serializedObject.Update();
76+
prop.serializedObject.ApplyModifiedProperties();
77+
}
78+
79+
public static void SetValue(this SerializedProperty serializedProperty, object value) {
3480
switch (serializedProperty.propertyType)
3581
{
3682
case SerializedPropertyType.Integer:
@@ -106,6 +152,12 @@ public static void SetValue(this SerializedProperty serializedProperty, object v
106152
case SerializedPropertyType.Hash128:
107153
serializedProperty.hash128Value = (Hash128)value;
108154
break;
155+
case SerializedPropertyType.ManagedReference:
156+
serializedProperty.managedReferenceValue = value;
157+
break;
158+
case SerializedPropertyType.Generic:
159+
serializedProperty.SetValueReflective(value);
160+
break;
109161
default:
110162
Debug.LogWarning(
111163
$"Tried to copy value '{value}' from a template to an SOC item but apparently that's not supported.");

0 commit comments

Comments
 (0)