Creating a Modular Progress Display

I needed to create a display recently to show the progress of cacheing a remote Addressable Asset in Unity, which got me thinking about how many times I’ve needed (and probably will need) something reusable and modular like this for the games I’ve worked on.

The script’s still a bit of a work in progress, but essentially it makes use of an enumerated type in order to either update an Image component’s fillAmount variable, a TextMeshProUGUI component’s text, or both. In most cases this is all I need it to update, particularly now that I’m often using the assets from a really nice Modern UI pack on the Asset Store. The script has a couple of public methods which can be accessed by other scripts, such as a generic ‘FillOverTime’ coroutine which takes in a float for its duration (which uses a while loop and Mathf.Lerp to transition between empty and filled).

Most importantly, it has a public DisplayVal method which can be called within the while loop of another script, for instance a custom DownloadHandler class (allowing us to display a download’s progress). I’ve sometimes had it where a while loop process doesn’t always neatly go from 0 to 100, and might leave off at, say, 87. In that event, the ‘CompleteFill’ coroutine can be called to fill in the remaining amount, just so it makes a bit more sense for the user. This takes in an optional float argument, otherwise it’ll default to a duration of 2f.

With this all set up, we just need to create an empty GameObject within the scene, drag the script onto it, and prefab it in order to have this available to us as a tool for whenever we need it. If we wanted to get super fancy with this, we could probably even find a way to set this up as part of a Scriptable Object with a CreateAssetMenu attribute, allowing us to right click in the hierarchy or asset panel and create one of these whenever we need it.

We drag in the image and/or the text we want it to refer to, set the enum, then drag this into a slot in the inspector for another class. I recommend having a slot/variable for this in the script you want to use it in, but perform a null check whenever you go to use it (that way, you don’t need to specify this, but if you do it’ll work). This should give us a more modular set up, allowing us to add, remove, and ultimately use this as we need it.

As mentioned, the script’s still a bit of a work in progress, but I’ll be including it below in case it’d be of use!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store