Created
November 18, 2017 20:51
-
-
Save jasonweimanntsunami/cfc32df11252ea1e797562694a160ac9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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