// Cinema Suite using UnityEngine; namespace CinemaDirector { /// /// Transition from White to Clear over time by overlaying a guiTexture. /// [CutsceneItem("Transitions", "Fade from White", CutsceneItemGenre.GlobalItem)] public class FadeFromWhite : CinemaGlobalAction { private Color From = Color.white; private Color To = Color.clear; /// /// Setup the effect when the script is loaded. /// void Awake() { if (guiTexture == null) { gameObject.transform.position = Vector3.zero; gameObject.transform.localScale = new Vector3(100, 100, 100); gameObject.AddComponent(); guiTexture.enabled = false; guiTexture.texture = new Texture2D(1, 1); guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height); guiTexture.color = Color.clear; } } /// /// Enable the overlay texture and set the Color to White. /// public override void Trigger() { guiTexture.enabled = true; guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height); guiTexture.color = From; } /// /// Firetime is reached when playing in reverse, disable the effect. /// public override void ReverseTrigger() { End(); } /// /// Update the effect over time, progressing the transition /// /// The time this action has been active /// The time since the last update public override void UpdateTime(float time, float deltaTime) { float transition = time / Duration; FadeToColor(From, To, transition); } /// /// Set the transition to an arbitrary time. /// /// The time of this action /// the deltaTime since the last update call. public override void SetTime(float time, float deltaTime) { if (time >= 0 && time <= Duration) { guiTexture.enabled = true; UpdateTime(time, deltaTime); } else if (guiTexture.enabled) { guiTexture.enabled = false; } } /// /// End the effect by disabling the overlay texture. /// public override void End() { guiTexture.enabled = false; } /// /// The end of the action has been triggered while playing the Cutscene in reverse. /// public override void ReverseEnd() { guiTexture.enabled = true; guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height); guiTexture.color = To; } /// /// Disable the overlay texture /// public override void Stop() { if (guiTexture != null) { guiTexture.enabled = false; } } /// /// Fade from one colour to another over a transition period. /// /// The starting colour /// The final colour /// the Lerp transition value private void FadeToColor(Color from, Color to, float transition) { guiTexture.color = Color.Lerp(from, to, transition); } } }