While many times we just wish to display non-interactive text messages to the user, there are times (such as name entry for high scores) where we wish that the user was able to enter text or numbers into our game. Unity provides the Input Field UI component for this purpose. In this recipe, we'll create a simple text input UI by making use of the default Button image and text GameObjects, and we'll add a script to respond to each new value of the input field.
You can, of course, create a working text input quicker than this recipe's method by choosing menu: Create | UI | Input Field, which creates a GameObject containing an Input Field component, child text, and placeholder GameObjects, as shown in the following screenshot. However, by following the steps in this recipe, you'll learn the interrelationships between the different interface elements, because you'll be creating these connections manually from the deconstructed parts of the UI Button GameObject.
To create a promoted text input box to the user with faint placeholder text, follow these steps:
4
and Top to 7
.-50
.The core of interactive text input in Unity is the responsibility of the Input Field component. This needs a reference to a UI Text GameObject. To make it easier to see where the text can be typed, we have made use of the default rounded rectangle image that Unity provides when a Button GameObject is created. Buttons have both an Image component and a Text child GameObject. So, two items that we need can be acquired very easily by creating a new Button, and simply by removing the Button (Script) component.
There are usually three Text GameObjects involved with the user text input: the static prompt text (in our recipe, for example, the Name: text); then the faint placeholder text, reminding users where and what they should type; and finally the text object (with the font and color settings and so on) that is actually displayed to the user, showing the characters as they type.
At runtime, a Text-Input Input Caret GameObject is created—displaying the blinking vertical line to inform the user where their next letter will be typed. Note that the Content Type of the Input Field (Script), in the Inspector, can be set to several specific types of text input, including e-mail addresses, integer or decimal numbers only, or the password text (where an asterisk is displayed for each entered character).
There are some details that you don't want to miss.
Having interactive text on the screen isn't of much use unless we can retrieve the text entered to use in our game logic, and we may need to know each time the user changes the text content and act accordingly.
To add code and events to respond each time the text content has been changed by the user, do the following:
DisplayChangedTextContent
to the Text-input GameObject:using UnityEngine; using System.Collections; using UnityEngine.UI; public class DisplayChangedTextContent : MonoBehaviour { private InputField inputField; void Start(){ inputField = GetComponent<InputField>(); } public void PrintNewValue (){ string msg = "new content = '" + inputField.text + "'"; print (msg); } }
3.144.35.122