Game Development Reference
In-Depth Information
In the Project panel Assets ➤ Script folder, select Create and make a new script named
GUIBoxScript. Edit the code as follows:
#pragma strict
function OnGUI() {
GUI.Box(Rect (0, 75, 100, 50), "This is a box");
}
Save the script, attach it to the GUI game object, save the scene, and playtest. This time the text in
the box remains visible while that of the label fades away.
Image
You can display images instead of text. Edit GUIBoxScript to the following:
#pragma strict
public var boxTexture : Texture2D;
function OnGUI() {
GUI.Box(Rect (0, 75, 100, 50), boxTexture);
}
This breaks down as follows:
(1) public var boxTexture : Texture2D;
Declare a reference variable boxTexture of type Texture2D for the image file.
1.
(2) GUI.Box(Rect (0, 75, 100, 50), boxTexture);
2.
This line of code creates the GUI box control, but with an image parameter
instead of the string parameter used in the previous example.
Save the script. In the Unity editor, with the GUI game object selected you will see the Box Texture
property with an empty field appear in GUIBoxScript component within the Inspector. In the Project
panel, open the Assets ➤ Sample Assets ➤ Sample Scenes ➤ Textures folder. Drag the gui_reticule
image into the Box Texture property field. Save the scene, playtest, and the reticule image appears
in the box. Outstanding!
Remember, you can use +' with the cursor on GUI to open the Unity Scripting Reference and find
the various options available for customizing the contents of GUI.Box or any of the GUI controllers.
Text Input
At times you may want input from the player. The TextField and TextArea GUI controls serve this
purpose, where the only difference between them is that TextField holds one line of text and
TextArea can hold many.
 
Search WWH ::




Custom Search