Focus the input field initializing properties.
Handles what happens after a user selects an InputField. This is a protected property. To return the focus state use InputField.isFocused. To shift focus to another GameObject, use EventSystem.SetSelectedGameObject.
A common use of this is allowing the user to type once focussed. Another way is outputting a message when the user clicks on a field (often seen when creating passwords).
//Create an Input Field by going to Create>UI>Input Field. Attach this script to the Input Field GameObject
using UnityEngine; using UnityEngine.UI;
public class Example : MonoBehaviour { InputField m_InputField; void Start() { //Fetch the Input Field component from the GameObject m_InputField = GetComponent<InputField>(); }
void Update() { //Check if the Input Field is in focus and able to alter if (m_InputField.isFocused) { //Change the Color of the Input Field's Image to green m_InputField.GetComponent<Image>().color = Color.green; } } }
Did you find this page useful? Please give it a rating: