Game Development Reference
In-Depth Information
Use the
Mathf.Lerp()
function to smooth the transition of the alpha value,
then assign the new value to
GUI.contentColor.a
, which holds the reference
color for the GUI text to be rendered.
5.
(6) GUI.Label(Rect(0, 0, 250, 25), "Press C to crouch or Space to jump");
6.
Create a label whose upper left corner will be positioned in the upper left
corner of the platform screen, with the text instructions that will be displayed
using the newly calculated and assigned alpha value.
Save the script, save the scene, and playtest. You will see the message appear in the upper left
corner, then fade away over 3 seconds. You can change the length of time it stays visible by
adjusting
fadeDuration
in the Inspector.
Box
Add the following line of code to the end of the
OnGUI()
function to see the difference between a
Label and a Box:
GUI.Box(Rect (0, 75, 100, 50), "This is a box");
This works exactly like
GUI.Label
. Save the script and play. The dark background of the box is a little
difficult to see against the dark skybox of the Obstacle Course scene, so you may have to pan the
camera around a little to see it (Figure
11-3
).
Figure 11-3.
A GUI.Label and GUI.Box displayed in the Game view
Notice that the text for the label and the box both fade away. This is because
GUI.contentColor
in
the code you wrote for the fading message applies to all of the content in this
OnGUI()
function.
One way to address this is to use different scripts for the various GUI controls.
Remove the
GUI.Box(Rect (0, 75, 100, 50), "This is a box");
line of code and save the script.