Unity / C#
Beginner
Unity 2021.3+ / Unity 6
비동기 씬 로드(LoadSceneAsync)와 페이드 인/아웃 로딩 바 연동 컨트롤러Asynchronous Scene Loading (LoadSceneAsync) with Fade Transition and Progress Bar
화면 멈춤(Freeze) 현상 없이 다음 씬을 백그라운드에서 로딩하고 allowSceneActivation 제어로 100% 로드 시점에 부드럽게 화면을 열어주는 트랜지션 매니저.A transition manager that loads scenes asynchronously in the background without freezing the screen and coordinates fade-in/out transitions using allowSceneActivation.
#Unity #SceneManagement #Async #UI #Transition
핵심 개념
SceneManager.LoadSceneAsync는 0.0~0.9 구간 동안 데이터를 읽고, allowSceneActivation = false일 때 0.9에서 대기합니다. 이때 프로그레스 바를 1.0까지 보간한 뒤 활성화 플래그를 전환하면 자연스러운 씬 전환을 구현할 수 있습니다.Core Concept
SceneManager.LoadSceneAsync loads scene assets from 0.0 to 0.9 and pauses at 0.9 when allowSceneActivation = false. Interpolating the progress bar to 1.0 before enabling the activation flag produces a clean visual transition.1. 비동기 로딩 코루틴 구현1. Asynchronous Loading Coroutine Implementation
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;
}
}
}
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. Fade out (to black screen)
float t = 0f;
while (t < 1f)
{
t += Time.deltaTime * 2f;
fadeCanvasGroup.alpha = t;
yield return null;
}
// 2. Load scene asynchronously
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. Fade in
while (t > 0f)
{
t -= Time.deltaTime * 2f;
fadeCanvasGroup.alpha = t;
yield return null;
}
}
}
2. 실무 적용 및 주의사항2. Practical Considerations
- 전환 중 중복 입력 차단: 페이드 아웃이 시작될 때
fadeCanvasGroup.blocksRaycasts = true로 설정하여 UI 버튼의 중복 클릭을 막고, 페이드 인이 끝난 뒤false로 되돌려야 합니다. - 0.9 구간 진행률 정규화: Unity의 비동기 씬 로드는 에셋 로드 단계인
0.9까지만progress를 갱신하므로, 로딩 바 UI에는Mathf.Clamp01(op.progress / 0.9f)로 계산한 값을 표시하는 것이 자연스럽습니다. - 전환 매니저의 씬 간 유지:
SceneTransitionManager와 페이드용 UI 캔버스는 기존 씬이 언로드될 때 파괴되지 않도록 최상위 오브젝트에 배치하고DontDestroyOnLoad를 적용해야 페이드 인 단계까지 정상적으로 실행됩니다.
- Block Input During Transitions: Set
fadeCanvasGroup.blocksRaycasts = truewhen starting the fade-out to prevent duplicate button clicks, and reset it tofalseonce the fade-in finishes. - Normalize Progress Across the 0.9 Cap: Because Unity reports asynchronous loading progress up to
0.9before scene activation, displayMathf.Clamp01(op.progress / 0.9f)on the UI slider for a full 0-to-100% progression. - Persist the Transition Manager Across Scenes: Place
SceneTransitionManagerand its fade Canvas on a root GameObject withDontDestroyOnLoadso the coroutine continues running through the fade-in step after the previous scene unloads.
비동기 씬 로드와 CanvasGroup 기반 페이드 연출을 결합하면 씬 전환 시 발생하는 화면 멈춤 현상을 가리고 안정적인 로딩 흐름을 제공할 수 있습니다.
Combining asynchronous scene loading with a CanvasGroup fade overlay masks frame hitches during scene activation and provides a consistent loading flow.