/// Enumerators that fades in the canvas's imageComponent to turn the screen to a flat color over time. Fadeins called simeutaneously will only fade in the earliest call and discard any others.
/// </summary>
/// <param name="duration">How long it takes to the image to fade in.</param>
/// <param name="color">Target color for the image to reach.</param>
floattotalTime=0f;// Total amount of time this coroutine has taken. Determines when the fadein will end and what color the imageComponent should be at every frame.
while(totalTime<=duration)
{
totalTime+=Time.deltaTime;
_imageComponent.color=Color.Lerp(newColor(0,0,0,0),color,totalTime/duration);// Sets the image's color to a mixture between total transparency and the target color, and interpolates based on the amount of time to completion.
yieldreturnnull;
}
_imageComponent.color=color;// Here to guarentee the image is exactly the requested color at the end of the loop.
IsCurrentlyFading=false;
yieldreturnnull;
_fadeChannelSO.OnEventRaised+=InitiateFade;
}
privatevoidOnDisable()
{
_fadeChannelSO.OnEventRaised-=InitiateFade;
/// Enumerators that fades out the canvas's imageComponent to turn the screen to normal gameplay color over time. Fadeouts called simeutaneously will only fade out the earliest call and discard any others.
/// Enumerator that fades in the canvas's imageComponent to turn the screen to a flat color over time. Fadeins called simeutaneously will only fade in the earliest call and discard any others.
/// <param name="duration">How long it takes to the image to fade out.</param>
color=_imageComponent.color;// Stores the old color of the image component, as we can't assume the image will always be black, if no color is specified.
floattotalTime=0f;// Total amount of time this coroutine has taken. Determines when the fadeout will end and what color the imageComponent should be at every frame.
ColorstartColor=_imageComponent.color;
if(fadeIn)endColor=Color.clear;
floattotalTime=0f;
_imageComponent.color=Color.Lerp(color,newColor(0,0,0,0),totalTime/duration);// Sets the image's color to a mixture between the old color and total transparency, and interpolates based on the amount of time to completion.
if(!IsCurrentlyFading)// Makes sure multiple fade-ins or outs don't happen at the same time. Note this will mean fadeouts called at the same time will be discarded.
if(!_isCurrentlyFading)// Makes sure multiple fade-ins or outs don't happen at the same time. Note this will mean fadeouts called at the same time will be discarded.
IsCurrentlyFading=true;
if(fadeIn)
{
StartCoroutine(FadeInEnum(duration,color));
}
else
{
StartCoroutine(FadeOutEnum(duration,color));// Fadeout doesn't need color, so the color parameter is disregarded.