Western Shooter Devblog #14 | Building Easy UI in Unity
Building UI can sometimes be confusing or intimidating but I’m gonna show you how you can easily make a really quick and simple UI in Unity for our Western Shooter Project!
First thing you will need to do is make a Canvas, this will be our UI space where we can add UI elements completely separate from our world space!
For the type of UI we want, we are gonna make sure our UI Scale Mode is set to “Scale With Screen Size”, that way no matter what screen size the game is running in, the UI will automatically adjust so that all the UI stays where they should be.
Next we are gonna add our UI element, The first thing we will make will be a score counter, so to do this we will add some UI Text.
So as you can see here, even though I placed my text on the giant UI Canvas super far away from our game world space, it will render in the top-right corner of our game window! How cool is that?
Now to get this score counter actually working, let’s get into the coding side of this!
Let’s make a UIManager Script that can control our UI Text and make a method that will add to a score variable!
So if you want to be able to control UI Elements, the first thing you need to do is access the UI class by adding “using UnityEngine.UI;” at the top. Next you can see we’ll take reference to our Text through the inspector and then through our AddToScore Method, we’ll change our score text with the score variable that will change based on how many points are added when it’s called!
So where do we add to the score from? We’ll do this from the Enemy script so that whenever they are killed by the player, they will call the AddToScore() method adding 100 points, and then Destroy(gameObject).
And just like that! We have a quick and easy score system for our game!
And if we decide to put in the extra time and effort into making it look nicer, we can get something like this!
Next Devblog we are gonna look at using Unity UI for a Game Over Screen!