Input Fields On Unity

Input Fields are no strangers to us. We see it all the time as we are browsing the internet or using our favorite apps. With this we can capture user inputs to either make our user named there Player in our game, make the user enter his username or passwords, etc.

How dose Input Fields work on Unity?

Each Input Field have two event that you can use: OnValueChanged(String) and OnEndEdit(String).

Using OnValueChanged(String)

Create a Canvas with a UI InputField GameObject and a UI Text GameObject. Create a UIManager Script with the following variable and method and attach this script to the Canvas, also notice the using UnityEngine.UI. Drag the TEXT component to the script so you can have reference:

In the OnValueChanged section hit the plus sign and drag your Canvas that has the UIManager script and find the public function WhatIsYourAge

OnValueChanged will update the text as you type in the input field.

Using OnEndEdit(String)

Using the same steps as before but this time we will use OnEndEdit section.

With OnEndEdit you either need to hit Enter from your keyboard or press outside of the Input Field in order to update the text.

Using Button

You can also use a button if you have multiple input field and will like to send / update the text at the same time.

Add a Button Object to your Canvas and make the following changes to the UIManager script:

Notice that you will have to add the reference on both the Text that is going to hold the Output and the Text GameObject that is on the Input Field.

Bellow the Input Field there two type of Text Objects: Placeholder and Text. Placeholder help the user to know what to write on the field and the Text Object is the one that's going to hold the string. That Text Object will be the new reference that you will add to the UIManager script under _inputFieldText.

On the button add the Canvas and find the public method called Input Field.

Now you can also use the Input Field with a Button 😁

Now with a little bit of search you can customize your game or make a application that allow user input.

Hope it helps.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rehtse Studio

Rehtse Studio

Game Developer sharing content around Unity, programing and tips and tricks in game dev.