Forked from jasonweimanntsunami/ProgressSceneLoader.cs
Created
September 18, 2022 20:52
-
-
Save cherryboyjr/f5579a7a33b1a73ef3fe13af99c3f782 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