// Cinema Suite
using UnityEngine;
namespace CinemaDirector
{
///
/// Transition from Black to Clear over time by overlaying a GetComponent().
///
[CutsceneItem("Transitions", "Fade from Black", CutsceneItemGenre.GlobalItem)]
public class FadeFromBlack : CinemaGlobalAction
{
private Color From = Color.black;
private Color To = Color.clear;
///
/// Setup the effect when the script is loaded.
///
void Awake()
{
if (GetComponent() == null)
{
gameObject.transform.position = Vector3.zero;
gameObject.transform.localScale = new Vector3(100, 100, 100);
gameObject.AddComponent();
GetComponent().enabled = false;
GetComponent().texture = new Texture2D(1, 1);
GetComponent().pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
GetComponent().color = Color.clear;
}
}
///
/// Enable the overlay texture and set the Color to Black.
///
public override void Trigger()
{
GetComponent().enabled = true;
GetComponent().pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
GetComponent().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)
{
GetComponent().enabled = true;
UpdateTime(time, deltaTime);
}
else if (GetComponent().enabled)
{
GetComponent().enabled = false;
}
}
///
/// End the effect by disabling the overlay texture.
///
public override void End()
{
GetComponent().enabled = false;
}
///
/// The end of the action has been triggered while playing the Cutscene in reverse.
///
public override void ReverseEnd()
{
GetComponent().enabled = true;
GetComponent().pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
GetComponent().color = To;
}
///
/// Disable the overlay texture
///
public override void Stop()
{
if (GetComponent() != null)
{
GetComponent().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)
{
GetComponent().color = Color.Lerp(from, to, transition);
}
}
}