Game Development Reference
In-Depth Information
Using the
TextField
control is simple, as it returns the final state of the value that has
been entered and you have to pass that same variable as a parameter for the current
text to be displayed. To use the controls, you apply them in script as follows:
string textString1 = "Some text here";
string textString2 = "Some more text here";
string textString3 = "Even more text here";
void OnGUI() {
textString = GUI.TextField(new Rect(25, 100, 100, 30),
textString1);
textString = GUI.TextArea(new Rect(150, 100, 200, 75),
textString2);
textString = GUI.PasswordField(new Rect(375, 100, 90, 30),
textString3, '*');
}
A note about strings in Unity scripts
Strings are immutable. Every time you change their value they create a
new string in memory by having the
textString
variable declared at
the class level it is a lot more memory efficient.
If you declare the
textString
variable in the
OnGUI
method,
it will generate garbage (wasted memory) in each frame.
Worth keeping in mind.
When displayed, the textbox by default looks like this:
As with the
Label
and
Button
controls, the font of the text displayed can be altered
using either a
GUIStyle
or
guiText
GameObject property.
Note that text will overflow within the field if it is too large for
the display area, but it will not be drawn outside the
TextField
dimensions. The same goes for multiple lines.