FiE-Game/Assets/Cinema Director/Cutscene Items/Global Items/Transitions/FadeToBlack.cs

121 lines
4.1 KiB
C#
Raw Normal View History

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