[Image 1] 로딩 화면 UI
[Image 2] Hierarchy창에 표시된 다음 씬
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadSceneManager : MonoSingleton<LoadSceneManager>
{
public GameObject loadingPanel;
public Image progressBar;
public TMP_Text progressText;
private void Start()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
loadingPanel.SetActive(false);
}
public void LoadScene(string sceneName)
{
loadingPanel.SetActive(true);
StartCoroutine(LoadingAsync(sceneName));
}
IEnumerator LoadingAsync(string sceneName)
{
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName);
asyncOperation.allowSceneActivation = false;
float time = 0f;
float progress = 0f;
while (!asyncOperation.isDone)
{
yield return null;
time += Time.deltaTime;
if (progress >= 90f)
{
progress = Mathf.Lerp(progress, 100f, time);
if (progress == 100f)
{
asyncOperation.allowSceneActivation = true;
}
}
else
{
progress = Mathf.Lerp(progress, asyncOperation.progress * 100f, time);
if (progress >= 90f) time = 0f;
}
progressText.text = $"{progress}%";
progressBar.fillAmount = progress * 0.01f;
}
}
}