Simple (but complete) code example to create an input field in script (no editor actions please!)

I’m trying to find an example of the code required to add a text input field. I want to do this ALL IN CODE (without using the editor). I assume I need to include “UnityEngine.UI”?

I don’t need any bells and whistles or fancy backgrounds (yet). I just need the necessary code to create an input field that allows a user to input a text string and print the results to the console.


If you don’t know coding but need just a super basic input field then I would recommend oldest of Unity’s UI:

It’s something like >10 years old now and I have no idea for how long this grandpa-of-a-UI-system will be supported in the future but it still works. It’s super easy to understand and build simple things with but don’t take it too far as it is not very capable nor performant.

It’s full name is Immediate Mode GUI (IMGUI) and here is a list of ui elements it can display: