Page 1 of 3

Custom loading bar

Posted: Tue Nov 30, 2021 5:09 pm
by hrohibil
Hey Toni



From the doc if i want to use a custom loading screen, i basiclly just replaced the spinning flower logo with my own.

I followed a tutorial and got to below code.
How do i use or where exactly should this go "AsyncOperation PixelCrushers.SaveSystem.currentAsyncOperation"

Here is my code:

Code: Select all

using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;

public class levelLoaderBar : MonoBehaviour
{

    //   AsyncOperation PixelCrushers.SaveSystem.currentAsyncOperation

    public GameObject loadingScreen;
    public Slider slider;
    public TextMeshProUGUI progressText;
    public void LoadLevel(int sceneIndex)
    {

        StartCoroutine(loadAsync(sceneIndex));

    }

    IEnumerator loadAsync(int sceneIndex)
    {


        AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);



        loadingScreen.SetActive(true);
        while (!operation.isDone)
        {

            float progress = Mathf.Clamp01(operation.progress / .9f);
            Debug.Log(progress);

            slider.value = progress;
            progressText.text = progress * 100f + "%";

            yield return null;




        }

    }
}

AS you see i tried inserted it here, but i get errors.


Re: Custom loading bar

Posted: Tue Nov 30, 2021 6:55 pm
by Tony Li
Hi,

You don't need to do anything that that tutorial script does. The Save System is already loading the scene. You don't need to write another script to load it, too.

Add a Slider to your scene. Set its range to 0 .. 1. Add this script to it:

LoadingScreenProgressBar.cs

Code: Select all

using System.Collections;
using UnityEngine;
using PixelCrushers;

public class LoadingScreenProgressBar : MonoBehaviour
{
    IEnumerator Start()
    {
        var slider = GetComponent<UnityEngine.UI.Slider>();
        while (true)
        {
            slider.value = SaveSystem.currentAsyncOperation.progress;
            yield return null;
        }
    }
}

Re: Custom loading bar

Posted: Tue Nov 30, 2021 7:10 pm
by hrohibil
Thank you so much Toni. I will test it out.

If I want a text percentage I can still use this right:

progressText.text = progress * 100f + "%";

Re: Custom loading bar

Posted: Tue Nov 30, 2021 7:18 pm
by Tony Li
Yes. It may take a little adjustment, but that's the idea.

Re: Custom loading bar

Posted: Wed Dec 01, 2021 1:37 am
by hrohibil
So on the loadingscreen scene.
I created a slider and added your script to it.

I play main scene and when i clik a load button or start new game it should transisition to the loading screen scene, but i gt a black screen with following error



I also created a UI text with following reference, but i get a red curly error under the word "progress".
progressText.text = value * 100f + "%";

Re: Custom loading bar

Posted: Wed Dec 01, 2021 4:24 am
by hrohibil
This is my settings, I out your script on the slider it self.



This is the error I am getting when clicking from main scene to a load game or new game etc....


It is this line 20 it says error on..

Re: Custom loading bar

Posted: Wed Dec 01, 2021 8:42 am
by Tony Li
Hi,

That script is for a Slider. It should only be a on a GameObject that has a Slider.

Try changing that line to:

Code: Select all

if (slider != null && SaveSystem.currentAsyncOperation != null) slider.value = SaveSystem.currentAsyncOperation.progress;

Re: Custom loading bar

Posted: Wed Dec 01, 2021 9:53 am
by hrohibil
The script was already on a gameobject with a slider.
I just created the default unity UI slider and placed the script on the GO with the slider.

Please see this short clip. I have two questions

1:
The slider just waits a long time and then moves/jumps a long streak and then again. Like jaggy a bit,

2:;
How do i get my percentage to display the nunbers? I get no errors from the code..

Code: Select all

using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LoadingProgressBar : MonoBehaviour
{
    public Text progressText;

    IEnumerator Start()
    {

        var slider = GetComponent<UnityEngine.UI.Slider>();
        while (true)
        {
             float progress = Mathf.Clamp01(SaveSystem.progress / .9f);
             Debug.Log(progress);

            if (slider != null && SaveSystem.currentAsyncOperation != null) slider.value = SaveSystem.currentAsyncOperation.progress;

           
            progressText.text = progress * 100f + "%";
            yield return null;
        }
    }
}

Link to VIDEO;

Re: Custom loading bar

Posted: Wed Dec 01, 2021 11:48 am
by Tony Li
Hi,

The progress value will always be jaggy and inaccurate in the editor's play mode. This is the way Unity works. It will be smooth in builds.

For text percent, add a UI Text to the scene, and add this script to its GameObject:

LoadingProgressText.cs

Code: Select all

using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LoadingProgressText : MonoBehaviour
{
    IEnumerator Start()
    {
        var progressText = GetComponent<UnityEngine.UI.Text>();
        while (true)
        {
            if (progressText != null && SaveSystem.currentAsyncOperation != null)
            {
                progressText.text = Mathf.Clamp(SaveSystem.currentAsyncOperation.progress * 100f, 0f, 100f).ToString();
            }
            yield return null;
        }
    }
}

Re: Custom loading bar

Posted: Wed Dec 01, 2021 12:19 pm
by hrohibil
Thank you so much Toni.

You go such a long way and extra...

I hate to ask for help again, but i am getting a error on line 16. See image

Thank you for the help

EDIT: I did try to put a "ToString(); at the end, and that did clear the error.
But play time showed nothing, It just stayed at 0 %