// Use reflection to find the named float field public static TargetBinding BindTarget(GameObject target, string fieldName) { fieldName = fieldName.Trim(); TargetBinding binding = new TargetBinding(); GameObjectFieldScanner scanner = new GameObjectFieldScanner(); scanner.OnLeafField = (fullName, fieldInfo, rootFieldOwner, value) => { //Debug.Log(fullName); if (fullName == fieldName) { binding.mTargetFieldInfo = fieldInfo.ToArray(); binding.mTargetFieldOwner = new object[binding.mTargetFieldInfo.Length]; binding.mTargetFieldOwner[0] = rootFieldOwner; binding.mInitialValue = Convert.ToSingle(value); return(false); // abort scan, we're done } return(true); }; scanner.ScanFields(target); if (!binding.IsValid) { Debug.Log(string.Format( GetFullName(target) + " Reactor: can't find " + ((fieldName.Length == 0) ? "(empty)" : fieldName))); } return(binding); }
public static List <string> GetAllFieldOfType(Type type, GameObject go) { List <string> fields = new List <string>(); GameObjectFieldScanner scanner = new GameObjectFieldScanner(); scanner.OnFieldFound = (string fullName, FieldInfo field, ref object owner) => { //Debug.Log(fullName); if (!field.FieldType.IsSubclassOf(type)) { return(false); } fields.Add(fullName); return(true); }; scanner.ScanFields(go); return(fields); }
public static FieldInfo FieldFromPath(GameObject go, string path, out object outObj) { GameObjectFieldScanner scanner = new GameObjectFieldScanner(); FieldInfo resultFI = null; object resultObj = null; scanner.OnFieldFound = (string fullName, FieldInfo field, ref object owner) => { if (resultFI == null && fullName == path) { resultFI = field; resultObj = owner; return(true); } return(false); }; scanner.ScanFields(go); outObj = resultObj; return(resultFI); }