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
|
public static T SerializedPropertyToObject<T>(SerializedProperty property){ return GetNestedObject<T>(property.propertyPath, GetSerializedPropertyRootComponent(property), true); //The "true" means we will also check all base classes }
public static Object GetSerializedPropertyRootComponent(SerializedProperty property){ return property.serializedObject.targetObject; }
public static T GetNestedObject<T>(string path, object obj, bool includeAllBases = false){ //taoria: here the code has some issues. foreach (var part in path.Split('.')){ obj = GetFieldOrPropertyValue<object>(part, obj, includeAllBases); Debug.Log(obj); if (obj == null) continue;
if (obj.GetType().GetGenericTypeDefinition() == typeof(List<>)){ var first = path.LastIndexOf('['); var last = path.LastIndexOf(']'); var index = int.Parse(path.Substring(first + 1, last - first - 1)); var list = (List<T>) obj; return list[index]; break; } }
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){ var field = obj.GetType().GetField(fieldName, bindings); if (field != null) return (T) field.GetValue(obj); var property = obj.GetType().GetProperty(fieldName, bindings); if (property != null) return (T) property.GetValue(obj, null);
if (includeAllBases) foreach (var 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; }
public static void SetFieldOrPropertyValue<T>(string fieldName, object obj, object value, bool includeAllBases = false, BindingFlags bindings = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic){ var field = obj.GetType().GetField(fieldName, bindings); if (field != null){ field.SetValue(obj, value); return; }
var property = obj.GetType().GetProperty(fieldName, bindings); if (property != null){ property.SetValue(obj, value, null); return; }
if (includeAllBases) foreach (var type in GetBaseClassesAndInterfaces(obj.GetType())){ field = type.GetField(fieldName, bindings); if (field != null){ field.SetValue(obj, value); return; }
property = type.GetProperty(fieldName, bindings); if (property != null){ property.SetValue(obj, value, null); return; } } }
public static IEnumerable<Type> GetBaseClassesAndInterfaces(this Type type, bool includeSelf = false){ var 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()); //I found this on stackoverflow
return allTypes; }
|
近期评论