Creating a Progress Bar with the New Input System in Unity 2021

In this article we’ll look at controlling a slider (or progress) bar in real-time with the new input system.

When we hold down the spacebar, we want the slider bar to fill in and when we release the spacebar we want the slider to drain.

First of all, we’ll need to add the slider UI as shown above.

Second, we’ll need an Input asset to which we’ll add the Slider Action Map and the FillBar Action with a binding to the spacebar key.

Create a slider script and add it to the Slider UI object.
PRO TIP: Do not name your script “slider” as this will conflict with the UI.Slider class!

We’ll use this script to detect the input and then modify the “Value” field of the slider to fill the bar.

In our script we’ll add using UnityEngine.InputSystem and using UnityEngine.UI and then create a _slider variable to hold a reference to the slider UI object.

In the Start() method we’ll create our _inputActions from our PlayerInputActions file and enable the Slider Action Map of it.

In Update(), we simply check if Slider.FillBar.IsPressed is true or false and then add or subtract a small amount to the Slider UI’s “Value” field.

We also want to limit the slider Value field to be >= 0 and <= 1.

Now if we run our game, we can see that holding Spacebar increases the slider fill and releasing the spacebar drains it.



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