If you build it, they will come.
Ease of Building UI Elements in Unity’
Feedback on a games state is critical for the user's ability to make choices about how to interact with the game. Unity makes creating user interfaces easy and intuitive.
We start by adding a canvas to our hierarchy. They can do this by right clicking on the hierarchy window and choosing UI > Canvas. This will create a canvas object in our scene, and we will add our UI elements to this canvas that sits on top of our game by default. Creating the canvas also creates an EventSystem object which ties our user input system to actions performed on our UI elements.
There are many types of UI elements that we can add to our canvas. For the purposes of our space shooter we are going to start with a combination of text elements and images. Text elements can be sized and stylized to better suit our games and are added through the same UI menu that we added our canvas from. We can right click on our canvas object, to make sure our text elements is a child if the canvas, and select UI > Text. We will add some text displaying the players score.
Our image element is added in the same way, using Right Click > UI > Image. Image components are holders for rendering 2D sprites as UI elements. These can be positioned, sized, and even animated. For our purposes we have a set of sprites displaying our players life count.
We can control both our text elements and our image elements by fetching the associated components from the game objects that are attached to and changing the appropriate values for each. In this case we can create some serialized fields of type Text and Image respectively and drag the items from the hierarchy to gain access to them inside our scripts.
Unity’s UI system is easy to start using and offers the ability to build complex and layered interfaces if needed. Tomorrow we’ll look at creating a retro style game over screen. If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.