private void AddTransformRecorder() { memberReferences.Add(StratusMemberReference.Construct(() => transform.position)); memberReferences.Add(StratusMemberReference.Construct(() => transform.rotation)); memberReferences.Add(StratusMemberReference.Construct(() => transform.localScale)); memberReferences.Add(StratusMemberReference.Construct(() => transform.parent)); }
//------------------------------------------------------------------------/ // Methods: Static //------------------------------------------------------------------------/ /// <summary> /// Keeps watch of a given property/field /// </summary> /// <typeparam name="T"></typeparam> /// <param name="varExpr">An expression of a given variable of the form: '(()=> foo')</param> /// <param name="behaviour">The owner object of this variable</param> /// <example>Overlay.Watch(()=> foo, this);</example> public static void Watch <T>(Expression <Func <T> > varExpr, string description = null, MonoBehaviour behaviour = null) { StratusMemberReference variableRef = StratusReflection.GetReference(varExpr); Watcher watcher = new Watcher(variableRef, description, behaviour); instance.Windows.Watch.Add(watcher); }
/// <summary> /// Constructor /// </summary> /// <param name="prefix">A given prefix for this variable</param> /// <param name="variable">The variable being watched</param> public Watcher(StratusMemberReference variable, string description, MonoBehaviour behaviour) : base(variable.name) { Behaviour = behaviour; Variable = variable; // Whether to use the default variable name or a custom description if (description != null) { Description = description; } else { Description = variable.name; } }
/// <summary> /// Constructs a reference to the given member from a lambda expression capture /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expression"></param> /// <returns></returns> public static StratusMemberReference Construct <T>(Expression <Func <T> > expression) { // Use expressions to find the underlying owner object var memberExpr = expression.Body as MemberExpression; var inst = memberExpr.Expression; var targetObj = Expression.Lambda <Func <object> >(inst).Compile()(); var variableName = memberExpr.Member.Name; // Construct the member reference object StratusMemberReference memberReference = new StratusMemberReference(); memberReference.name = variableName; memberReference.target = targetObj; // Check if it's a property var property = targetObj.GetType().GetProperty(variableName); if (property != null) { memberReference.property = property; memberReference.type = property.PropertyType; memberReference.memberType = MemberType.Property; return(memberReference); } // Check if it's a field var field = targetObj.GetType().GetField(variableName); if (field != null) { memberReference.field = field; memberReference.type = field.FieldType; memberReference.memberType = MemberType.Field; return(memberReference); } // Invalid throw new ArgumentException("The given variable is neither a property or a field!"); }