Game Development Reference
In-Depth Information
In the GUIScript, experiment with changing the (x, y) values to relocate the label. When you also
experiment with the
Rect
size properties, you will find that when you make the label smaller, the text
is cut off and does not reduce in size along with the rectangle's dimensions.
Fading Out
Suppose at the start of the game you would like to inform the player how to make the player
character crouch and jump with a message that fades away after 3 seconds. Edit the GUIScript to
the following:
#pragma strict
public var fadeDuration : float = 3;
function OnGUI() {
var fadingColor : Color;
fadingColor = GUI.contentColor;
var fading : float = (Time.time/fadeDuration);
GUI.contentColor.a = Mathf.Lerp(1, 0, fading);
GUI.Label(Rect(0, 0, 250, 25), "Press C to crouch or Space to jump");
}
Breaking this down:
(1) public var fadeDuration : float = 3;
Declare a float type variable
fadeDuration
and assign it a value of 3.
1.
(2) var fadingColor : Color;
Declare a reference variable
fadingColor
of type
Color
. Recall that
Color
is made up of r, g, b, a values where a, the alpha value, represents the
transparency such that 1 is fully opaque and 0 is fully transparent.
2.
(3) fadingColor = GUI.contentColor;
Assign the current GUI text Color value held by the
GUI
class
contentColor
variable to the
fadingColor
variable.
3.
(4) var fading : float = (Time.time/fadeDuration);
Declare a float type variable
fading
to hold the result of the time since game
start divided by
fadeDuration
in seconds.
4.
(5) GUI.contentColor.a = Mathf.Lerp(1, 0, fading);