Game Development Reference
In-Depth Information
Naming controls
With each control that you implement through script, you can name them as you
place them; this is essential if you want to control flow and access to each field from
the keyboard or to derive logic based on the currently selected/focused control.
Now unlike most other Unity functionality, you cannot directly name controls,
there is no Name field on the properties of the controls as they are just commands
to the GUI system to draw things to the screen, kind of like a rendering pipeline.
So to name GUI controls in Unity, we simply need to tell the GUI system that the
next control we are going to draw has a name, as follows:
string login = "Would you like to play a game?";
void OnGUI() {
GUI.SetNextControlName("MyAwesomeField");
login = GUI.TextField(new Rect(10, 10, 200, 20), login);
}
Getting in focus
With names defined on controls, you could then define which control you were
focusing on. To focus on a specific control, you would simply need to call:
GUI.FocusControl("MyAwesomeField");
This would then change the user's input focus or selection to the specific GUI control
with that name.
Once you have a control in focus, you then discover the name of the specific control
in focus by calling:
string selectedControl = GUI.GetNameOfFocusedControl();
If the control in focus has a name, it will return the name you set for that control.
If no control is in focus or the control in focus has no name, it will return an
empty string.
The logon example
As an example of using the previous naming and focus capabilities, you can
build a simple logon GUI for a user to enter with validation behavior and some
usability features.
To demonstrate, we will create a user registration form where the user can enter a
username and password to register with your game. The password however will have
to be more than six characters long for security reasons (no weak passwords here).
 
Search WWH ::




Custom Search