using CinemaDirector.Helpers; using System.Collections.Generic; using System.Reflection; using UnityEngine; namespace CinemaDirector { /// /// An Event for enabling any behaviour that has an "enabled" property. /// [CutsceneItemAttribute("Game Object", "Enable Behaviour", CutsceneItemGenre.ActorItem)] public class EnableBehaviour : CinemaActorEvent, IRevertable { // The Component/Behaviour to Enable. public Component Behaviour; // Options for reverting in editor. [SerializeField] private RevertMode editorRevertMode = RevertMode.Revert; // Options for reverting during runtime. [SerializeField] private RevertMode runtimeRevertMode = RevertMode.Revert; /// /// Cache the state of all actors related to this event. /// /// All the revert info related to this event. public RevertInfo[] CacheState() { List actors = new List(GetActors()); List reverts = new List(); foreach (Transform go in actors) { if (go != null) { Component b = go.GetComponent(Behaviour.GetType()) as Component; if (b != null) { PropertyInfo pi = Behaviour.GetType().GetProperty("enabled"); bool value = (bool)pi.GetValue(b, null); reverts.Add(new RevertInfo(this, Behaviour, "enabled", value)); } } } return reverts.ToArray(); } /// /// Trigger this event and Enable the chosen Behaviour. /// /// The actor to perform the behaviour enable on. public override void Trigger(GameObject actor) { Component b = actor.GetComponent(Behaviour.GetType()) as Component; if (b != null) { Behaviour.GetType().GetMember("enabled"); PropertyInfo fieldInfo = Behaviour.GetType().GetProperty("enabled"); fieldInfo.SetValue(Behaviour, true, null); } } /// /// Reverse trigger this event and Disable the chosen Behaviour. /// /// The actor to perform the behaviour disable on. public override void Reverse(GameObject actor) { Component b = actor.GetComponent(Behaviour.GetType()) as Component; if (b != null) { PropertyInfo fieldInfo = Behaviour.GetType().GetProperty("enabled"); fieldInfo.SetValue(b, false, null); } } /// /// Option for choosing when this Event will Revert to initial state in Editor. /// public RevertMode EditorRevertMode { get { return editorRevertMode; } set { editorRevertMode = value; } } /// /// Option for choosing when this Event will Revert to initial state in Runtime. /// public RevertMode RuntimeRevertMode { get { return runtimeRevertMode; } set { runtimeRevertMode = value; } } } }