Game Development Reference
In-Depth Information
Display Text
The most fundamental manner in which the GUI communicates with the user is by displaying text.
The label and box are very simple controls that do just this. In fact, the only difference between a
label and a box is that a label displays only the text, while a box is a label with a dark background
around it.
Label
To create a label, first you will need a script. In the Project panel, open the Assets ➤ Scripts folder,
then create a new script and name it GUIScript. Open GUIScript in MonoDevelop and edit the code
to the following:
#pragma strict
function OnGUI() {
GUI.Label(Rect(0, 0, 125, 25), "A label displays text");
}
This code breaks down as follows:
(1) function OnGUI()
The OnGUI() function used for creating GUI controls is called every frame.
1.
(2) GUI.Label(Rect(0, 0, 125, 25), "A label displays text");
Call the Label() function of the GUI class and pass it the x and y screen
coordinates, the width and height of the label followed by the text to be
displayed in the label.
2.
Save the script, attach it to the GUI game object, save the scene, and run. You will see the label text
displayed in the upper left corner of the Game view (Figure 11-1 ).
 
Search WWH ::




Custom Search