[Unity / C#] AsyncOperation

주예성·2025년 7월 24일

Unity 간판부수기

목록 보기
2/3
post-thumbnail

세이브 및 로딩 작업을 하다가 발견한 클래스, AsyncOperation 클래스입니다.

AsyncOperation란?

  • 유니티에서 비동기 작업을 처리하는 클래스
  • 시간이 오래 걸리는 작업(씬 로딩, 에셋 로딩 등)을 백그라운드에서 처리
  • 메인 스레드를 막지 않고 진행상황을 추적할 수 있음

주요 속성들

AsyncOperation asyncLoad = SceneManager.LadSceneAsync("MainScene");

// 진행률 (0.0 ~ 1.0, 단 씬 로딩은 0.9까지만)
float progress = asyncLoad.progress;

// 작업 완료 여부
bool isFinished = asyncLoad.isDone;

// 씬 자동 활성화 여부
asyncLoad.allowSceneActivation = false;

// 우선순위 설정 (높을수록 빠름)
asyncLoad.priority = ThreadPriority.High;

다른 유용한 함수들

1. 에셋 비동기 로딩:

ResourceRequest request = Resources.LoadAsync<GameObject>("PlayerPrefab");
while (!request.isDone)
{
    Debug.Log($"에셋 로딩: {request.progress * 100}%");
    yield return null;
}
GameObject player = request.asset as GameObject;

2. 씬 언로드:

AsyncOperation unloadOp = SceneManager.UnloadSceneAsync("OldScene");

3. 에셋번들 로딩:

AssetBundleCreateRequest bundleRequest = AssetBundle.LoadFromFileAsync(path);

실제 활용 예시

IEnumerator LoadWithControl()
{
    AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("MainScene");
    asyncLoad.allowSceneActivation = false; // 수동 제어
    
    while (asyncLoad.progress < 0.9f) // 0.9까지만 로딩
    {
        loadingText.text = $"로딩: {asyncLoad.progress * 100:F0}%";
        yield return null;
    }
    
    loadingText.text = "완료! 클릭하세요";
    
    // 사용자 입력 대기
    while (!Input.GetMouseButtonDown(0))
        yield return null;
    
    asyncLoad.allowSceneActivation = true; // 씬 활성화
}

이 클래스는 비동기 작업의 상태를 추적하고 제어하는 핵심 클래스입니다!

profile
Unreal Engine & Unity 게임 개발자

0개의 댓글