Unity / C#
Beginner
Unity 2021.3+ / Unity 6
비동기 씬 로드(LoadSceneAsync)와 페이드 인/아웃 로딩 바 연동 컨트롤러
화면 멈춤(Freeze) 현상 없이 다음 씬을 백그라운드에서 로딩하고 allowSceneActivation 제어로 100% 로드 시점에 부드럽게 화면을 열어주는 트랜지션 매니저.
#Unity #SceneManagement #Async #UI #Transition
핵심 개념
SceneManager.LoadSceneAsync는 0.0~0.9 구간 동안 데이터를 읽고, allowSceneActivation = false일 때 0.9에서 대기합니다. 이때 프로그레스 바를 1.0까지 보간한 뒤 활성화 플래그를 켜주어야 끊김 없는 전환이 완성됩니다.1. 비동기 로딩 코루틴 구현
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneTransitionManager : MonoBehaviour
{
[SerializeField] private CanvasGroup fadeCanvasGroup;
[SerializeField] private Slider progressBar;
public void ChangeScene(string sceneName)
{
StartCoroutine(LoadSceneRoutine(sceneName));
}
private IEnumerator LoadSceneRoutine(string sceneName)
{
// 1. 페이드 아웃 (검은 화면)
float t = 0f;
while (t < 1f)
{
t += Time.deltaTime * 2f;
fadeCanvasGroup.alpha = t;
yield return null;
}
// 2. 비동기 씬 로드
AsyncOperation op = SceneManager.LoadSceneAsync(sceneName);
op.allowSceneActivation = false;
while (op.progress < 0.9f)
{
progressBar.value = op.progress;
yield return null;
}
progressBar.value = 1f;
yield return new WaitForSeconds(0.2f);
op.allowSceneActivation = true;
// 3. 페이드 인
while (t > 0f)
{
t -= Time.deltaTime * 2f;
fadeCanvasGroup.alpha = t;
yield return null;
}
}
}