Start a new topic
Answered

UnityPlugin: Import progress bar

While importing asynchronously I´d like to have a progressbar.

So I added this line of code into the provided onProgressChanged method:

 

void onProgressChanged(float progress, string message) {

Debug.Log("Progress : " + 100f * progress + "%");
mySlider.value = progress;
}


Because of asynchronously importing I get an UnityException: 

get_isPlaying can only be called from the main thread.


What can I do?



Best Answer

For example, like this:



private float _mainValue = 0;

void Update()
{
    slider.value = _mainValue;
}

void onProgressChanged(float progress, string message)
{
    _mainValue = progress;
    Debug.Log("Progress : " + 100f * progress + "%");
}


In this example, the slider (UI) value is set in the main thread. And the value it is taken is updated by the import asynchronous thread. So no UI directly updated in an asynchronous thread!

Hi Carsten!

Thanks for contacting us!


This is because you cannot make UI modifications on an other thread than the main one in Unity. 

You should look into passing this information to the main thread and then update your UI.


Best,

Nicolas

Yes, cool, but how?
Answer

For example, like this:



private float _mainValue = 0;

void Update()
{
    slider.value = _mainValue;
}

void onProgressChanged(float progress, string message)
{
    _mainValue = progress;
    Debug.Log("Progress : " + 100f * progress + "%");
}


In this example, the slider (UI) value is set in the main thread. And the value it is taken is updated by the import asynchronous thread. So no UI directly updated in an asynchronous thread!

Ok, I see, this works - thank you.

As it seems , the onProgressChanged method is only called at fixed precentage values, like 40, 60, 80, 90%.

Any way to get true percentage integers like 0,1,2,3,...., 100?

Unfortunately, this progress value is not as precised as we would like it to be. So we only return the important steps of the process.

If you want to get a more linear progress bar, I suggest you do something like this:


    private float _mainValue = 0;

    private void Start()
    {
        slider.minValue = 0;
        slider.maxValue = 100;
    }

    void Update()
    {
        if (slider.value < _mainValue) {
            slider.value++;
        }
    }

    void onProgressChanged(float progress, string message)
    {
        _mainValue = progress * 100;
        Debug.Log("Progress : " + 100f * progress + "%");
    }





1 person likes this

Thank you!

Login or Signup to post a comment