/// <summary> /// Returns false if the field was constant (literal) while setting 'result' to null. /// Otherwise true while setting result to a new RuntimeMember wrapping the specified field /// using the appropriate method of building the [s|g]etters (delegates in case of editor/standalone, reflection otherwise) /// </summary> public static bool TryWrapField(FieldInfo field, object target, out RuntimeMember result) { if (field.IsLiteral) { result = null; return(false); } result = new RuntimeMember(field, field.FieldType, target); #if DYNAMIC_REFLECTION result._setter = field.DelegateForSet(); result._getter = field.DelegateForGet(); #else result._setter = field.SetValue; result._getter = field.GetValue; #endif return(true); }
public virtual void Reset() { var members = RuntimeMember.CachedWrapMembers(GetType()); for (int i = 0; i < members.Count; i++) { var member = members[i]; member.Target = this; var defAttr = member.Info.GetCustomAttribute <DefaultAttribute>(); if (defAttr != null) { var value = defAttr.Value; if (value == null && !member.Type.IsAbstract) // null means to instantiate a new instance { value = member.Type.ActivatorInstance(); } member.Value = value; } } }
/// <summary> /// Returns false if the property isn't readable or if it's an indexer, setting 'result' to null in the process. /// Otherwise true while setting result to a new RuntimeMember wrapping the specified property /// using the appropriate method of building the [s|g]etters (delegates in case of editor/standalone, reflection otherwise) /// Note that readonly properties (getter only) are fine, as the setter will just be an empty delegate doing nothing. /// </summary> public static bool TryWrapProperty(PropertyInfo property, object target, out RuntimeMember result) { if (!property.CanRead || property.IsIndexer()) { result = null; return(false); } result = new RuntimeMember(property, property.PropertyType, target); if (property.CanWrite) { #if DYNAMIC_REFLECTION result._setter = property.DelegateForSet(); #else result._setter = (x, y) => property.SetValue(x, y, null); #endif } #if DYNAMIC_REFLECTION else { result._setter = delegate(ref object obj, object value) { } };
public static void ResetTarget(object target) { var type = target.GetType(); #if DBG Log("Assigning default values if any to " + type.Name); #endif var members = RuntimeMember.EnumerateMemoized(type); for (int i = 0; i < members.Count; i++) { var member = members[i]; var defAttr = member.Info.GetCustomAttribute <DefaultAttribute>(); if (defAttr != null) { member.Target = target; var value = defAttr.Value; if (value == null && !member.Type.IsAbstract) { value = member.Type.ActivatorInstance(); } member.Set(value); } } }
private static UnityObject ResolveFromTargetRelative(object target, GameObject gameObject, RuntimeMember member, RequiredFromRelativeAttribute attribute, Func <string, GameObject> GetOrAddRelativeAtPath, Func <string, GameObject> GetRelativeAtPath, Func <Type, Component> GetComponentInRelative) { if (member.Type == typeof(GameObject)) { var path = ProcessPath(attribute.Path, member.Name); if (path.IsNullOrEmpty()) { return(null); } if (attribute.Create) { return(GetOrAddRelativeAtPath(path)); } try { return(GetRelativeAtPath(path)); } catch { return(null); } } else { Component c = null; var path = attribute.Path; if (path.IsNullOrEmpty()) { c = GetComponentInRelative(member.Type); } else { path = ProcessPath(attribute.Path, member.Name); GameObject relative; if (attribute.Create) { relative = GetOrAddRelativeAtPath(path); } else { try { relative = GetRelativeAtPath(path); } catch { relative = null; } } if (relative == null) { return(null); } c = relative.GetComponent(member.Type); if (c == null && attribute.Add) { if (member.Type.IsAbstract) { Debug.Log("Can't add component `" + member.Type.Name + "` because it's abstract"); } else { c = relative.AddComponent(member.Type); } } } return(c); } }
private static UnityObject ResolveFromTargetParents(object target, GameObject gameObject, RuntimeMember member, RequiredAttribute attribute) { return(ResolveFromTargetRelative(target, gameObject, member, attribute as RequiredFromRelativeAttribute, gameObject.GetOrAddParentAtPath, gameObject.GetParentAtPath, gameObject.GetComponentInParent)); }
private static UnityObject ResolveFromTargetGO(object target, GameObject gameObject, RuntimeMember member, RequiredAttribute attribute) { if (member.Type == typeof(GameObject)) { return(gameObject); } var c = gameObject.GetComponent(member.Type); if (c == null) { if ((attribute as RequiredFromThisAttribute).Add) { #if dbg Debug.Log("adding component " + memberType.Name + " to " + gameObject.name); #endif c = gameObject.AddComponent(member.Type); } } return(c); }
private static UnityObject ResolveSingle(object target, GameObject gameObject, RuntimeMember member, RequiredAttribute attribute) { var single = (attribute as RequiredSingleAttribute).FromResources ? Resources.FindObjectsOfTypeAll(member.Type).FirstOrDefault() : UnityObject.FindObjectOfType(member.Type); #if dbg Debug.Log("found single " + single + " for " + memberType.Name + " in " + target); #endif return(single); }
private static UnityObject DontResolve(object target, GameObject gameObject, RuntimeMember member, RequiredAttribute attribute) { return(null); }