public static bool ValuesEqual <T>(this T component1, T component2) where T : Component { ReflectionHelperMethods reflectionHelper = new ReflectionHelperMethods(); foreach (var variable in reflectionHelper.GetAllVariables(typeof(T))) { //Early Skip if (variable.Name == "name" || variable.Name == "hideFlags" || variable.Name == "tag") { continue; } var component1Value = Convert.ChangeType(reflectionHelper.GetValue(variable, component1), reflectionHelper.GetVariableType(variable)); var component2Value = Convert.ChangeType(reflectionHelper.GetValue(variable, component2), reflectionHelper.GetVariableType(variable)); //component1Value = Convert.ChangeType(component1Value, reflectionHelper.GetVariableType(variable)); //component2Value = Convert.ChangeType(component2Value, reflectionHelper.GetVariableType(variable)); if (component1Value == null && component2Value != null) { return(false); } if (component1Value != null && component1Value.Equals(component2Value) == false) { Debug.Log("Found discrepancy with variable " + variable.Name + ". First value was " + component1Value + ", while second was " + component2Value); return(false); } } return(true); }
private MethodInfo FindEditorUpdateMethod(MonoBehaviour mb) { MethodInfo method = null; method = mb.GetType().GetMethod("EditorUpdate", BindingFlags.Public | BindingFlags.Instance); if (method == null) { //let's try to see if it's a private method declared in one of the parent classes ReflectionHelperMethods rhm = new ReflectionHelperMethods(); method = rhm.GetNonPublicMethodInBaseClasses(mb.GetType(), "EditorUpdate"); } return(method); }
private static IEnumerable <PropertyInfo> GetPropertiesWithAuto(MonoBehaviour mb) { ReflectionHelperMethods rhm = new ReflectionHelperMethods(); return(mb.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(prop => prop.PropertyType.IsPrimitive == false) .Where(prop => Attribute.IsDefined(prop, typeof(AutoAttribute)) || Attribute.IsDefined(prop, typeof(AutoChildrenAttribute)) || Attribute.IsDefined(prop, typeof(AutoParentAttribute)) ) .Concat( rhm.GetNonPublicPropertiesInBaseClasses(mb.GetType()) .Where(prop => prop.PropertyType.IsPrimitive == false) .Where(prop => Attribute.IsDefined(prop, typeof(AutoAttribute)) || Attribute.IsDefined(prop, typeof(AutoChildrenAttribute)) || Attribute.IsDefined(prop, typeof(AutoParentAttribute)) ) )); }
private static IEnumerable <FieldInfo> GetFieldsWithAuto(MonoBehaviour mb) { ReflectionHelperMethods rhm = new ReflectionHelperMethods(); return(mb.GetType() .GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) .Where(prop => prop.FieldType.IsClass) .Where(prop => Attribute.IsDefined(prop, typeof(AutoAttribute)) || Attribute.IsDefined(prop, typeof(AutoChildrenAttribute)) || Attribute.IsDefined(prop, typeof(AutoParentAttribute)) ) .Concat( rhm.GetNonPublicFieldsInBaseClasses(mb.GetType()) .Where(prop => prop.FieldType.IsClass) .Where(prop => Attribute.IsDefined(prop, typeof(AutoAttribute)) || Attribute.IsDefined(prop, typeof(AutoChildrenAttribute)) || Attribute.IsDefined(prop, typeof(AutoParentAttribute)) ) )); }
public StateMachine(StateMachineRunner engine, MonoBehaviour component) { this.engine = engine; this.component = component; //Define States var values = Enum.GetValues(typeof(T)); if (values.Length < 1) { throw new ArgumentException("Enum provided to Initialize must have at least 1 visible definition"); } stateLookup = new Dictionary <object, StateMapping>(); for (int i = 0; i < values.Length; i++) { var mapping = new StateMapping((Enum)values.GetValue(i)); stateLookup.Add(mapping.state, mapping); } //Reflect methods MethodInfo[] allMethods = null; var publicMethods = component.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | /* BindingFlags.NonPublic |*/ BindingFlags.FlattenHierarchy) .Where(m => m.Name.Contains('_')); ReflectionHelperMethods rhm = new ReflectionHelperMethods(); var nonPublicMethods = rhm.GetNonPublicMethodsInBaseClasses(component.GetType()) .Where(m => m.Name.Contains('_') ).ToArray(); allMethods = publicMethods.Concat(nonPublicMethods).ToArray(); //var allFields = component.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | // BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Where(fieldInfo => IsIMXEvent(fieldInfo)); //Bind methods to states var separator = "_".ToCharArray(); for (int i = 0; i < allMethods.Length; i++) { if (allMethods[i].GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Length != 0) { continue; } var names = allMethods[i].Name.Split(separator); //Ignore functions without an underscore if (names.Length <= 1) { continue; } Enum key; try { key = (Enum)Enum.Parse(typeof(T), names[0]); } catch (ArgumentException) { //Not an method as listed in the state enum continue; } var targetState = stateLookup[key]; switch (names[1]) { case "Enter": if (allMethods[i].ReturnType == typeof(IEnumerator)) { targetState.hasEnterRoutine = true; targetState.EnterRoutine = CreateDelegate <Func <IEnumerator> >(allMethods[i], component); } else { targetState.hasEnterRoutine = false; targetState.EnterCall = CreateDelegate <Action>(allMethods[i], component); } break; case "Exit": if (allMethods[i].ReturnType == typeof(IEnumerator)) { targetState.hasExitRoutine = true; targetState.ExitRoutine = CreateDelegate <Func <IEnumerator> >(allMethods[i], component); } else { targetState.hasExitRoutine = false; targetState.ExitCall = CreateDelegate <Action>(allMethods[i], component); } break; case "Finally": targetState.Finally = CreateDelegate <Action>(allMethods[i], component); break; case "Update": targetState.Update = CreateDelegate <Action>(allMethods[i], component); break; case "LateUpdate": targetState.LateUpdate = CreateDelegate <Action>(allMethods[i], component); break; case "FixedUpdate": targetState.FixedUpdate = CreateDelegate <Action>(allMethods[i], component); break; case "OnCollisionEnter": targetState.OnCollisionEnter = CreateDelegate <Action <Collision> >(allMethods[i], component); break; } } //Create nil state mapping currentState = new StateMapping(null); }