Level fade-in technique
(as described in the Unity Game Development Essentials book - every beginner Unity developer should read it)
In Photoshop or other image-processing software create a plain white texture with a size of 64 by 64 pixels (width and height).
Select the texture now and in the Inspector, deselect Generate Mip Maps in the
Texture Importer component to stop Unity from rendering smaller versions of it
for 3D usethen press the Apply button at the bottom to confirm the change.
Create a new JavaScript. Rename this script FadeTexture. Double-click the script's icon to
launch it in the script editor.
Begin by establishing two variables, one a public member and the other a
private variable:
var theTexture : Texture2D;
private var StartTime : float;
The first variable here will hold the white texture asset ready to be stretched over the
screen, and the second is a floating-point number which you will use to store a value
of time.
Next, capture the value of Time.time in the StartTime variable when the level loads
by adding the following function to your script beneath the variables you just added:
function OnLevelWasLoaded(){
StartTime = Time.time;
}
You need to use this variable to capture the current amount of time elapsed, because
Time.time starts from when the first scene is loadedthat is, the menu or your first level. Because the
player will view your game menu or the first level first, you know that Time.time will not equal 0 when some other scene is loaded. You will use this variable shortly to subtract from the
current reading of time and thereby get a point to count from.
Now in the Update() function, place in the following code:
if(Time.time-StartTime >= 3){
Destroy(gameObject);
}
This if statement checks the value of Time.time, minus the time it was at when
the current scene loadedStartTimeand if it is more than or equal to 3, then you
destroy the game object this script is attached to. This simply removes the game
object you will attach this script to after three secondsas after the fade effect occurs,
you no longer need the object in the scene.
UnityGUI texture rendering
Establish a new OnGUI() function in your script, and place in the following lines
of code:
function OnGUI(){
GUI.color = Color.white;
GUI.color.a = Mathf.Lerp(1.0, 0.0, (Time.time-StartTime));
GUI.DrawTexture(Rect( 0, 0, Screen.width,
Screen.height ), theTexture);
}
Here you are addressing the GUI class directly. In the first line, you address the color
parameter, setting it to an in-built reference of the Color class called "white".
Then with this color set, you address its alpha parameterits visibilityby saying
GUI.color.a. You use the Mathf.Lerp command, interpolating the alpha from 1.0 (fully visible) to 0.0 (invisible).
The third parameter in the loop is the amount to interpolatebecause you are using
Time.time-StartTime here, you are effectively starting a counter from 0.0 which will
increase as time passes, so the Lerp occurs over time, creating the fade.
The third line actually renders the texture itself, using the DrawTexture command of
the GUI class. By specifying a Rect (rectangular space) for this to be drawn, starting
at 0,0the top left of the screenyou make sure that this texture stretches to fill
the screen by using Screen.width and Screen.height. This makes the fade work
in whichever resolution the player runs the game at, as it automates the size of the
texture on screen.
Go to File | Save in the script editor, and return to Unity now.
Back in Unity, go to GameObject | Create Empty. This creates a new object in the
Hierarchy called GameObject. Rename this to Fader now. Add the script you have
just written by going to Component | Scripts | Fade Texture. This adds the script as
a component. Now, simply drag-and-drop the white texture from your Project panel to the public member variable theTexture in the Inspector.
You are done! In order to create a fade-out effect create the reverse of the method described above. When you create the transition between your levels, apply the fade-out effect to the current level, then run your code (Application.LoadLevel (sceneToLoad);) and in the new scene will start with the fade-in effect described above.