facebook twitter hatena line email

Unity/Csharp/画面遷移/非同期

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

非同期画面遷移サンプル

public class SampleAsyncScene : MonoBehaviour
{
    [SerializeField] Button button;
    void Start()
    {
        button.onClick.AddListener(OnClickButton);
    }
    void OnClickButton()
    {
        StartCoroutine(LoadSceneAsync("SubScene"));
    }
    IEnumerator LoadSceneAsync(string sceneName)
    {
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
        asyncLoad.allowSceneActivation = false;
        Debug.Log("asyncLoad.progress=" + asyncLoad.progress + " asyncLoad.isDone=" + asyncLoad.isDone);
        // 遷移後にasyncLoad.isDoneがtrueになる
        while (!asyncLoad.isDone)
        {
            // asyncLoad.progressは0.9以上上がらないので・・
            if (asyncLoad.progress >= 0.9f)
            {
                asyncLoad.allowSceneActivation = true;
            }
            yield return null;
        }
    }
}

参考

公式:https://docs.unity3d.com/jp/current/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

https://nosystemnolife.com/unity_loading/

https://joyplot.com/documents/unity-load-scene-time/