Game Development Reference
In-Depth Information
So let's go through them in more detail:
All the following code is implemented in the sample project in the
basic GUI script located in the
Assets\Scripts
folder of the
downloadable code.
To experiment yourself, create a new project, scene, and script,
placing the code for each control in the script and attach the
script to the camera (by dragging it from the project view on to
the
Main Camera
GameObject in the scene hierarchy). You can
then either run the project or adorn the class in the script with the
[ExecuteInEditMode]
attribute to see it in the game view.
The Label control
Most GUI systems start with a
Label
control; this simply provides a stylized control
to display read-only text on the screen, it is initiated by including the following
OnGUI
method in your script:
void OnGUI() {
GUI.Label(new Rect(25, 15, 100, 30), "Label");
}
This results in the following on-screen display:
The
Label
control supports altering its font settings through the use of the
guiText
GameObject property (
guiText.font
) or
GUIStyles
. (See the following section on
GUIStyles
for more detail.)
To set
guiText.font
in your script, you would simply apply the
following in your script, either in the
Awake/Start
functions or
before drawing the next section of text you want drawn in another font:
public Font myFont = new Font("arial");
guiText.font = myFont;
You can also set the
myFont
property in
Inspector
using an
imported font.
The
Label
control forms the basis for all controls to display text, and as such,
all other controls inherit from it and have the same behaviors for styling the
displayed text.