Game Development Reference
In-Depth Information
Exploring
UnityScript
and
the
GUIButton object
The Unity Editor is laid out with an internal GUI-specification language called Un-
ityGUI. This API is only accessible to Unity game programmers from within C# (and
JavaScript) code, unlike the previous GUI elements that can be placed and adjus-
ted within the editor itself at design time. We can use this to place buttons, textures,
pop-up windows, tooltips, and many other UI primitives. The difference in use from
the previous examples is that the elements are instanced and placed entirely from the
script rather than at design time in the editor. For our dialog pop-up Prefabs, we will
explore the GUI.Button class.
Using UnityGUI
To use the UnityGUI functionality, we must invoke the commands from within a special
callback, OnGUI() . As this function is not created by default when you create a new
C# script in Unity, you need to add it yourself. The Unity Engine will invoke this meth-
od automatically when GUI elements are to be redrawn; so, we put our GUIButton
code and GUITexture and GUIText update code in here:
void OnGUI() { // insert code here }
Creating a clickable button
To create a button, use the GUI namespace and instantiate a button with parameters
into the constructor. There are six different function signatures one can use to instanti-
ate a GUI button, depending on which visuals or string you want to display on it. Each
type, however, requires the rect class as the first parameter:
GUI.Button( new rect(x,y,width,height), string);
Note that the new rect class instance takes the x , y (position of the rect's upper-left
corner) as well as the width and height dimensions of the button as the input.
Search WWH ::




Custom Search