Recently there was a question on Unity Answers about how to make an input field that only allows numeric values to be filled in. The solution is quite simple, especially if your making using of new UI stuff thats available since Unity-Version 4.6.
So let me show you how you can do it!
- Start by opening up your existing or a new Project file
- Insert an InputField into your scene
By executing this step a parent element called Canvas will be placed in the scene. If you have a look at the child object you should find an object named Input Field.
- Change the inspector type from Normal to Debug, that way we have access to more options of our textfield. (Right clicking on the inspector header should bring up the following menu)
- Scroll down to the Input Field Script. You should find there an option called Validation.
- Set it to float or int depending on the value that you wanted to be allowed to insert.
- Test it: Change to the Play-view and hit the Play button. If you now try to insert anything non numeric like (“a or b”). It shouldn’t appear in the input field.
Thats it for this Quicktip, I hope you did enjoy. Feel free to comment and share. 🙂