Game Development Reference
In-Depth Information
This coroutine is very simple and yet so powerful; walking through it, what happens is as
follows:
1. When the fade starts, we define a fading value and set it to zero.
2. Then, we run a while loop that runs until our fade value is 1 (full fade). In this
loop, we perform the following actions:
◦ Wait for the last frame to be drawn (keeps it smooth)
◦ Update our fade value based on how much time has passed against how
long the fade should last
◦ We use the Mathf.Clamp01 function to ensure the value does not go
above a certain range, limiting it to a maximum value
◦ Then, we use the drawing function we created earlier to draw a plane/
quad to the screen using our fading value as the alpha value
3. When fading out has completed, we load the next level as normal. However, we
check whether we have used an index or a name for the scene selection.
4. Finally, we repeat step 2, but this time fading in instead of out by looping our fad-
ing value to 0.
5. When finished, we set the fading flag to false to indicate that the script execu-
tion is complete.
Now, it is very important how we launch this coroutine because it could be interrupted at
any time, either by the existing game or by another fade being requested before the last
fade finished. To be able to stop it from anywhere in the game, we need to ensure it is
only launched using its string name. This allows us to use the StopAllCoroutines
function to kill it.
Tip
As stated in the previous chapter, if you have long running coroutines, always ensure they
can be started using their string names. Use method/delegate names only for short-lived
coroutines.
To show coroutines, the following is the method used to start the fading process:
private void StartFade(float aFadeOutTime, float
aFadeInTime, Color aColor)
{
fading = true;
Instance.fadeOutTime = aFadeOutTime;
Search WWH ::




Custom Search