Unity2D DevLog 04 — UI Ammo Counter
--
Last time, I shared an article on how I improved my shield powerup. It’s now time to work on an ammo counter. Infinite firepower is a feature of some games, but I want to include some limitations.
Objective:
Create a replenishable ammunition system for the player. Include a way to display ammo information on the UI.
How I went about it:
I had already planned for this feature in my UI.
The space UI element for the ammo counter is ready. All I need now are some graphic elements for the numbers.
Rather than using pre-made fonts, I chose to create graphics for the numbers. I had to make sure the numbers matched the style of the background art.
After importing and slicing the Sprite Sheet, I used individual Image Elements to place each number in the UI.
The Post Processing effects already help to make the numbers look illuminated.
Now comes the fun part. I created a new Script to handle my AmmoCounter. The first thing I added was two Sprite Type Arrays to hold my numbered sprites. The neat thing about this is that each sprite’s index corresponds with the number in the image.
I have two separate sprite sheets. You can see the subtle differences in the images above.
I also added four separate Image variables. Each of these variables represents one of the numbers in the UI.
I then created a public method to update the ammo counter on screen. This method takes an int parameter which refers to the remaining ammunition the player currently has. Using this information, I can change the current sprite displayed in each element with this code.
Just as the comments in the code suggest, I can break down the value of the passed integer to use as the index for each sprite.
In my Player Shooting Script, I created variables for the ammunition values. When the player fires a bullet, the method reduces this value. The AmmoCounter Script will then update the UI with the new ammunition count.
Conclusion:
I added some color changes to my ammo counter to inform the player that they are using a different weapon type. Ammunition is now a valuable resource. The player will have to think twice about spamming the Fire Button. I’ll make sure to balance this out with plenty of opportunities to reload.
What do you guys think?