// Cinema Suite using System; using System.Reflection; using UnityEngine; namespace CinemaDirector.Helpers { /// /// Holds info related to reverting objects to a former state. /// public class RevertInfo { private MonoBehaviour MonoBehaviour; private Type Type; private object Instance; private MemberInfo[] MemberInfo; private object value; /// /// Set up a revert info for a static object. /// /// The MonoBehaviour that is making this RevertInfo. /// The type of the static object /// The member name of the field/property/method to be called on revert. /// The current value you want to save. public RevertInfo(MonoBehaviour monoBehaviour, Type type, string memberName, object value) { this.MonoBehaviour = monoBehaviour; this.Type = type; this.value = value; this.MemberInfo = Type.GetMember(memberName); } /// /// Set up Revert Info for an instance object. /// /// The MonoBehaviour that is making this RevertInfo. /// The instance of the object you want to save. /// The member name of the field/property/method to be called on revert. /// The current value you want to save. public RevertInfo(MonoBehaviour monoBehaviour, object obj, string memberName, object value) { this.MonoBehaviour = monoBehaviour; this.Instance = obj; this.Type = obj.GetType(); this.value = value; this.MemberInfo = Type.GetMember(memberName); } /// /// Revert the given object to its former state. /// public void Revert() { if (MemberInfo != null && MemberInfo.Length > 0) { if (MemberInfo[0] is FieldInfo) { FieldInfo fi = (MemberInfo[0] as FieldInfo); if (fi.IsStatic || (!fi.IsStatic && Instance != null)) { fi.SetValue(Instance, value); } } else if (MemberInfo[0] is PropertyInfo) { PropertyInfo pi = (MemberInfo[0] as PropertyInfo); //if (Instance != null) { pi.SetValue(Instance, value, null); } } else if (MemberInfo[0] is MethodInfo) { MethodInfo mi = (MemberInfo[0] as MethodInfo); if (mi.IsStatic || (!mi.IsStatic && Instance != null)) { object[] values = new object[] { value }; mi.Invoke(Instance, values); } } } } /// /// Should we apply this revert in runtime. /// public RevertMode RuntimeRevert { get { return (MonoBehaviour as IRevertable).RuntimeRevertMode; } } /// /// Should we apply this revert in the editor. /// public RevertMode EditorRevert { get { return (MonoBehaviour as IRevertable).EditorRevertMode; } } } }