Skip to content

Instantly share code, notes, and snippets.

@jasonweimanntsunami
Created November 18, 2017 20:51
Show Gist options
  • Save jasonweimanntsunami/cfc32df11252ea1e797562694a160ac9 to your computer and use it in GitHub Desktop.
Save jasonweimanntsunami/cfc32df11252ea1e797562694a160ac9 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