UIs are the visual components of any computer system. The mouse cursor, folder icons, and programs on your laptop are all UI elements. For our game, we want a simple display to let our players know how many items they've collected, their current health, and a textbox to give them updates when certain events happen.
UI elements in Unity can be added in the following two ways:
- Directly from the Create menu in the Hierarchy panel, just as with any other GameObject
- Using the built-in GUI class in code
We want to stick to the code version for this project and add in our three UI elements in the GameBehavior class. This isn't to say that one approach is better than the other, but since we are learning to program, it's a good idea to stay consistent.
The GUI class provides several methods to create and position components; all GUI method calls go in a MonoBehaviour method called OnGUI(). Think of OnGUI() as the Update() method for all things UI; it runs anywhere...