Game Development Reference
In-Depth Information
Toggle
The toggle switch takes a slightly different pattern in its initiation because it always holds a boolean
value representing whether it is “on” or “off.” In the Project panel Assets ➤ Scripts folder create a
new script named GUIToggleAndSliders. Open it in MonoDevelop and edit the code to the following:
#pragma strict
var toggle : boolean = false;
function OnGUI () {
toggle = GUI.Toggle (Rect (Screen.width - 100, Screen.height - 100, 100, 30), toggle,
"Toggle switch");
Debug.Log(toggle);
}
This breaks down as follows:
(1) var toggle : boolean = false;
Declare a private boolean variable toggle to hold the current state of the
toggle switch.
1.
(2) toggle = GUI.Toggle (Rect (Screen.width - 100, Screen.height - 100, 100, 30),
toggle, "Toggle switch");
2.
You've seen this code configuration earlier with TextField and TextArea,
where the variable is assigned the value of the control that is created all in
one line. The pattern for a toggle is: position, size, toggle state, and the string
of text to appear next to the toggle switch.
(3) Debug.Log(toggle);
3.
Display the current state of the toggle switch in the Console.
Save the script, attach it to the GUI game object and playtest. As you select and deselect the toggle,
the corresponding state of the toggle switch is printed to the Console with each frame.
Sliders
Sliders have a sliding knob that can be moved from one end of the slider to the other along the scale
you assign it, where the ends of the slider represent the minimum and maximum values of the scale.
Sliders can be oriented horizontally or vertically.
 
Search WWH ::




Custom Search