// Cinema Suite using CinemaDirector.Helpers; using System.Collections.Generic; using UnityEngine; namespace CinemaDirector { /// /// Utility action for disabling a lot of GameObjects at once and then re-enabling them at the end of the action. /// [CutsceneItem("Utility", "Mass Disabler", CutsceneItemGenre.GlobalItem)] public class MassDisabler : CinemaGlobalAction, IRevertable { // Game Objects to be disabled temporarily. public List GameObjects = new List(); // Game Object Tags to be disabled temporarily. public List Tags = new List(); // Cache the game objects. private List tagsCache = new List(); // Options for reverting in editor. [SerializeField] private RevertMode editorRevertMode = RevertMode.Revert; // Options for reverting during runtime. [SerializeField] private RevertMode runtimeRevertMode = RevertMode.Revert; /// /// Cache the initial state of the target GameObject's active state. /// /// The Info necessary to revert this event. public RevertInfo[] CacheState() { List gameObjects = new List(); foreach (string tag in Tags) { GameObject[] tagged = GameObject.FindGameObjectsWithTag(tag); foreach (GameObject gameObject in tagged) { gameObjects.Add(gameObject); } } gameObjects.AddRange(GameObjects); List reverts = new List(); foreach (GameObject go in gameObjects) { if (go != null) { reverts.Add(new RevertInfo(this, go, "SetActive", go.activeInHierarchy)); } } return reverts.ToArray(); } /// /// Trigger this Action and disable all Game Objects /// public override void Trigger() { tagsCache.Clear(); foreach (string tag in Tags) { GameObject[] gameObjects = GameObject.FindGameObjectsWithTag(tag); foreach (GameObject gameObject in gameObjects) { tagsCache.Add(gameObject); } } setActive(false); } /// /// End the action and re-enable all game objects. /// public override void End() { setActive(true); } /// /// Trigger the beginning of the action while playing in reverse. /// public override void ReverseTrigger() { End(); } /// /// Trigger the end of the action while playing in reverse. /// public override void ReverseEnd() { Trigger(); } /// /// Enable/Disable all the game objects. /// /// Enable or Disable private void setActive(bool enabled) { // Enable gameobjects foreach (GameObject gameObject in GameObjects) { gameObject.SetActive(enabled); } // Enable tags foreach (GameObject gameObject in tagsCache) { gameObject.SetActive(enabled); } } /// /// 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; } } } }