// Cinema Suite using UnityEngine; namespace CinemaDirector { /// /// An action that fades in a texture over the first 25% of length, shows for 50% of time length /// and fades away over the final 25%. /// [CutsceneItem("GUITexture", "Fade Texture", CutsceneItemGenre.GlobalItem)] public class FadeTexture : CinemaGlobalAction { // The GUITexture to show public GUITexture target; // Optional Tint public Color tint = Color.grey; /// /// Disable the Texture and make it clear. /// void Awake() { if (this.target != null) { this.target.enabled = false; this.target.color = Color.clear; } } /// /// Trigger this event, enable the texture and make it clear. /// public override void Trigger() { if (this.target != null) { this.target.enabled = true; this.target.color = Color.clear; } } /// /// Reverse the start of this action by disabling the texture. /// public override void ReverseTrigger() { End(); } /// /// Update the fading/showing of this texture. /// /// The time of this action. /// The deltaTime since last update. public override void UpdateTime(float time, float deltaTime) { if (this.target != null) { float transition = time / Duration; if (transition <= 0.25f) { FadeToColor(Color.clear, tint, (transition / 0.25f)); } else if (transition >= 0.75f) { FadeToColor(tint, Color.clear, (transition - 0.75f) / .25f); } } } /// /// Update this action to an arbitrary time. /// /// The new time. /// The deltaTime since last update. public override void SetTime(float time, float deltaTime) { if (this.target != null) { this.target.enabled = true; if (time >= 0 && time <= Duration) { UpdateTime(time, deltaTime); } else if (target.enabled) { this.target.enabled = false; } } } /// /// End this action and disable the texture. /// public override void End() { if (this.target != null) { this.target.enabled = false; } } /// /// Trigger the action from the end in reverse. /// public override void ReverseEnd() { Trigger(); } /// /// Stop this action and disable its texture. /// public override void Stop() { if (this.target != null) { this.target.enabled = false; } } /// /// Fade between two colours over a transition value. /// /// The start color. /// The end color. /// The transition amount. private void FadeToColor(Color from, Color to, float transition) { this.target.color = Color.Lerp(from, to, transition); } } }