본문 바로가기
개발/C#

유니티 C# 씬 로드 동기, 비동기 간단 사용법 Load Scene Async

by SPNK 2022. 6. 21.
반응형

씬 동기 로드

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Example : MonoBehaviour
{

    public void LoadScene(int number)
    {
        SceneManager.LoadScene(number);
    }

    public void LoadScene(string name)
    {
        SceneManager.LoadScene(name);
    }
}

 

 

씬 비동기 로드

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Example : MonoBehaviour
{
    public Image sprite;
    public Text progressLabel;

    public string sceneName = "";

    public void LoadScene(string name)
    {
        sceneName = name;
        StartCoroutine(Load());
    }

    IEnumerator Load()
    {
        AsyncOperation async = SceneManager.LoadSceneAsync(sceneName);
        while (!async.isDone)
        {
            float progress = async.progress * 100.0f;
            int pRounded = Mathf.RoundToInt(progress);
            sprite.fillAmount = async.progress;
            progressLabel.text = (pRounded.ToString() + "%");

            yield return true;
        }
    }
}
반응형

댓글