/
SerializedPropertyExtensions.cs
186 lines (165 loc) · 7.07 KB
/
SerializedPropertyExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// --------------------------------------------------------------------------------------------------------------------
// <author>
// HiddenMonk
// http://answers.unity3d.com/users/496850/hiddenmonk.html
//
// Johannes Deml
// send@johannesdeml.com
// </author>
// --------------------------------------------------------------------------------------------------------------------
namespace VRTK.Supyrb
{
using System;
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Extension class for SerializedProperties
/// See also: http://answers.unity3d.com/questions/627090/convert-serializedproperty-to-custom-class.html
/// </summary>
public static class SerializedPropertyExtensions
{
/// <summary>
/// Get the object the serialized property holds by using reflection
/// </summary>
/// <typeparam name="T">The object type that the property contains</typeparam>
/// <param name="property"></param>
/// <returns>Returns the object type T if it is the type the property actually contains</returns>
public static T GetValue<T>(this SerializedProperty property)
{
return GetNestedObject<T>(property.propertyPath, GetSerializedPropertyRootComponent(property));
}
/// <summary>
/// Set the value of a field of the property with the type T
/// </summary>
/// <typeparam name="T">The type of the field that is set</typeparam>
/// <param name="property">The serialized property that should be set</param>
/// <param name="value">The new value for the specified property</param>
/// <returns>Returns if the operation was successful or failed</returns>
public static bool SetValue<T>(this SerializedProperty property, T value)
{
object obj = GetSerializedPropertyRootComponent(property);
//Iterate to parent object of the value, necessary if it is a nested object
string[] fieldStructure = property.propertyPath.Split('.');
for (int i = 0; i < fieldStructure.Length - 1; i++)
{
obj = GetFieldOrPropertyValue<object>(fieldStructure[i], obj);
}
string fieldName = fieldStructure.Last();
return SetFieldOrPropertyValue(fieldName, obj, value);
}
/// <summary>
/// Get the component of a serialized property
/// </summary>
/// <param name="property">The property that is part of the component</param>
/// <returns>The root component of the property</returns>
public static Component GetSerializedPropertyRootComponent(SerializedProperty property)
{
return (Component)property.serializedObject.targetObject;
}
/// <summary>
/// Iterates through objects to handle objects that are nested in the root object
/// </summary>
/// <typeparam name="T">The type of the nested object</typeparam>
/// <param name="path">Path to the object through other properties e.g. PlayerInformation.Health</param>
/// <param name="obj">The root object from which this path leads to the property</param>
/// <param name="includeAllBases">Include base classes and interfaces as well</param>
/// <returns>Returns the nested object casted to the type T</returns>
public static T GetNestedObject<T>(string path, object obj, bool includeAllBases = false)
{
foreach (string part in path.Split('.'))
{
obj = GetFieldOrPropertyValue<object>(part, obj, includeAllBases);
}
return (T)obj;
}
public static T GetFieldOrPropertyValue<T>(string fieldName, object obj, bool includeAllBases = false, BindingFlags bindings = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
{
FieldInfo field = obj.GetType().GetField(fieldName, bindings);
if (field != null)
{
return (T)field.GetValue(obj);
}
PropertyInfo property = obj.GetType().GetProperty(fieldName, bindings);
if (property != null)
{
return (T)property.GetValue(obj, null);
}
if (includeAllBases)
{
foreach (Type type in GetBaseClassesAndInterfaces(obj.GetType()))
{
field = type.GetField(fieldName, bindings);
if (field != null)
{
return (T)field.GetValue(obj);
}
property = type.GetProperty(fieldName, bindings);
if (property != null)
{
return (T)property.GetValue(obj, null);
}
}
}
return default(T);
}
public static bool SetFieldOrPropertyValue(string fieldName, object obj, object value, bool includeAllBases = false, BindingFlags bindings = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
{
FieldInfo field = obj.GetType().GetField(fieldName, bindings);
if (field != null)
{
field.SetValue(obj, value);
return true;
}
PropertyInfo property = obj.GetType().GetProperty(fieldName, bindings);
if (property != null)
{
property.SetValue(obj, value, null);
return true;
}
if (includeAllBases)
{
foreach (Type type in GetBaseClassesAndInterfaces(obj.GetType()))
{
field = type.GetField(fieldName, bindings);
if (field != null)
{
field.SetValue(obj, value);
return true;
}
property = type.GetProperty(fieldName, bindings);
if (property != null)
{
property.SetValue(obj, value, null);
return true;
}
}
}
return false;
}
public static IEnumerable<Type> GetBaseClassesAndInterfaces(this Type type, bool includeSelf = false)
{
List<Type> allTypes = new List<Type>();
if (includeSelf)
{
allTypes.Add(type);
}
if (type.BaseType == typeof(object))
{
allTypes.AddRange(type.GetInterfaces());
}
else
{
allTypes.AddRange(
Enumerable
.Repeat(type.BaseType, 1)
.Concat(type.GetInterfaces())
.Concat(type.BaseType.GetBaseClassesAndInterfaces())
.Distinct());
}
return allTypes;
}
}
}