M
M
Maxim Bukharin2021-11-06 01:22:41
C++ / C#
Maxim Bukharin, 2021-11-06 01:22:41

How to make a fill indicator in Unity when a button is clicked?

Need help.
I can't get the fill indicator.
I need to press the button and the bar (indicator) fills up in a certain time and during this time it was impossible to interrupt the download by pressing the button .

Can anyone help? I watched a lot of videos, searched for information, but everything does not work for the button.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Ente, 2021-11-06
@MM2one

You create two gameobjects - one with a button, the other with a slider. In the slider, you turn off the ability to control through the mouse and remove unnecessary elements to get a slider with filling. You set MinMax = 0, 1;
Then you start the script ProgressBar.cs and hang it on the Slider.

[SerializeField] private Button activationButton;
[SerializeField] private Slider progressSlider;
[SerializeField] private float speed = 1;

private void OnEnable()
{
     activationButton.onClick.AddListener(Press);
}

private void OnDisable()
{
     activationButton.onClick.RemoveListener(Press);
}

private void Press()
{
    activationButton.onClick.RemoveListener(Press);
    StartCoroutine(Loop());
    IEnumerator Loop()
    {
        for (var i=progressSlider.value; i<1f; i+= Time.deltaTime * speed)
        {
             progressSlider.value = i;
             yield return null;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question