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"
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:
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.