Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cherryboyjr/f5579a7a33b1a73ef3fe13af99c3f782 to your computer and use it in GitHub Desktop.
Save cherryboyjr/f5579a7a33b1a73ef3fe13af99c3f782 to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ProgressSceneLoader : MonoBehaviour
{
[SerializeField]
private Text progressText;
[SerializeField]
private Slider slider;
private AsyncOperation operation;
private Canvas canvas;
private void Awake()
{
canvas = GetComponentInChildren<Canvas>(true);
DontDestroyOnLoad(gameObject);
}
public void LoadScene(string sceneName)
{
UpdateProgressUI(0);
canvas.gameObject.SetActive(true);
StartCoroutine(BeginLoad(sceneName));
}
private IEnumerator BeginLoad(string sceneName)
{
operation = SceneManager.LoadSceneAsync(sceneName);
while (!operation.isDone)
{
UpdateProgressUI(operation.progress);
yield return null;
}
UpdateProgressUI(operation.progress);
operation = null;
canvas.gameObject.SetActive(false);
}
private void UpdateProgressUI(float progress)
{
slider.value = progress;
progressText.text = (int)(progress * 100f) + "%";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment