Journal 108 — Unity 2D Mobile, Dungeon Escape HUD and Android Setup

Objective: to add the onscreen HUD for gameplay and set up Android

In this article, we will add the heads up display (HUD) for the onscreen controls like direction and attack and jump, and life bar for mobile applications, and we will load a first build to Android.

We can start with a new UI panel.

Once created, delete the Image component, and create a child UI Image for the life bar graphic.

Move the graphic to the upper left of the game screen.

Next, we can add the joystick image below the life graphic.

Now, we can add the two A and B buttons.

We can add 4 life units to the life bar graphic. Use an empty game object to hold the 4 life bars. We want to first position the 4 life units to fix their position, then drag them in as children to the empty game object.

Next, we can add the gem count text to the player and life bar graphic. Note there is a custom font in the provided assets to use for the gem text.

We can update the UIManager with a new gem count function. We can drag the gem count text variable from the HUD to the UIManager.

In the Player script, we need to add a new function that updates the gem amount and then updates this for the UI.

Now, we can see if this updates in game.

Player Health

Now that the HUD is laid out, we can add the functionality for the life system.

We already have a health system so we can set the player with 4 health units, and update its Damage function.

We can update the player script to remove a health, and pass this lives info to the UIManager. We can also call for a death animation that we added earlier in the PlayerAnimation script. Also to stop an endless loop of death animation, we can return out of hte function if the player health is 0.

Now, we can check this in action.

Android Build

We can now try a demo of the game in Android. In Unity Build Settings, make sure the platform is set to Android, or otherwise select “Switch Platform”. When ready, select “Build”.

If any errors pop up, you may need to search online if you have the latest Android development or Java development kits. Using Unity 2021, the program includes JDK and SDK tools with Unity.

Once the game is built you can go to the storage location on your computer and then post the .apk file to an online file storage location like Dropbox, Google Drive, or OneDrive.

Once added to a file storage location, you can go to the app on the Android phone. In this example I am using Dropbox and a Samsung Galaxy S9+.

After opening Dropbox on the phone, I can select the file and save it to a storage location on the phone. Then I can open the Files app and navigate to the download location and select the file to install. There may be a notification that the file is unsafe, and you are directed to the file settings that allow you to change the settings for “Install unknown apps”. Then you can install the app.

Now you can see how it looks on the phone. In my case I adjusted the B button icon to bring it a little higher on screen.

Thank you for your time!

In the next article, I will go over the first setup of touch controls with the Input System.

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.