Custom loading bar

Announcements, support questions, and discussion for the Dialogue System.
hrohibil
Posts: 358
Joined: Thu Nov 04, 2021 12:50 pm

Custom loading bar

Post 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.

User avatar
Tony Li
Posts: 21079
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom loading bar

Post 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;
        }
    }
}
hrohibil
Posts: 358
Joined: Thu Nov 04, 2021 12:50 pm

Re: Custom loading bar

Post 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 + "%";
User avatar
Tony Li
Posts: 21079
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom loading bar

Post by Tony Li »

Yes. It may take a little adjustment, but that's the idea.
hrohibil
Posts: 358
Joined: Thu Nov 04, 2021 12:50 pm

Re: Custom loading bar

Post 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 + "%";
hrohibil
Posts: 358
Joined: Thu Nov 04, 2021 12:50 pm

Re: Custom loading bar

Post 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..
User avatar
Tony Li
Posts: 21079
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom loading bar

Post 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;
hrohibil
Posts: 358
Joined: Thu Nov 04, 2021 12:50 pm

Re: Custom loading bar

Post 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;
User avatar
Tony Li
Posts: 21079
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom loading bar

Post 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;
        }
    }
}
hrohibil
Posts: 358
Joined: Thu Nov 04, 2021 12:50 pm

Re: Custom loading bar

Post 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 %

Post Reply