Game Development Reference
In-Depth Information
TextField
In the Project panel Assets ➤ Scripts folder, create a new script named GUITextInputScript. Open it
in MonoDevelop and edit the code to the following:
#pragma strict
var textFieldString : String = "A one-line text field";
function OnGUI () {
textFieldString = GUI.TextField (Rect (0, 130, 120, 30), textFieldString);
}
This code breaks down as follows:
(1) public var textFieldString : String = "A one-line text field";
Declare the String type textFieldString variable and assign it an initial
message string “A one-line text field”.
1.
(2) textFieldString = GUI.TextField (Rect (0, 130, 120, 30), textFieldString);
Remember that OnGUI() is called every frame. The String variable
textFieldString will reflect the contents of the GUI.TextField as the
player edits it.
2.
Save the script, attach it to the GUI game object, and see it appear as a script component in
the Inspector. You can edit the initial message directly in the Text Field String property within the
Inspector, or watch it change during playtesting to reflect any changes made to the GUI.TextField
during gameplay.
TextArea
A TextArea works just like a TextField. In MonoDevelop, edit the GUITextInputScript to the following:
#pragma strict
var textFieldString : String = "A one-line text field";
var textAreaString : String = "A multi-line text field";
function OnGUI () {
textFieldString = GUI.TextField (Rect (0, 130, 120, 30), textFieldString);
textAreaString = GUI.TextArea (Rect (0, 175, 75, 75), textAreaString);
}
Save the script and playtest. Now the TextArea will wrap around the new lines of text that occupy its
space when you add more.
 
Search WWH ::




Custom Search