Unity 異步加載場景

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using Babybus;

public class LoadingManager : MonoBehaviour
{
    public static int               nextSceneId = -1;

    public int                      sceneId = -1;

    public GameObject               loadingSceneCanvas;

    private IEnumerator Start()
    {
        //yield return new WaitForSeconds(1);
        BabySystem.ShowAdView();
        yield return null;


        sceneId = sceneId < 0 ? nextSceneId : sceneId;
        if (sceneId == -1)
            sceneId = 0;

        var ao = SceneManager.LoadSceneAsync(sceneId);
        ao.allowSceneActivation = false;

        while (ao.progress < 0.9f)
        {
            yield return null;
        }
        yield return null;

        ao.allowSceneActivation = true;
    }

}

大概意思是這樣,如果allowSceneActivation 爲false , 進度條最終將停在0.9,然後IsDone 一直保存false,直到allowSceneActivation 被設置爲true,可以激活新場景。一般和LoadLevelAsync 與 LoadLevelAdditiveAsync組合使用。

注意加入上面那個0.9 改成大於0.9它就一直在while這個循環內,也就是不會跳轉。

https://docs.unity3d.com/ScriptReference/AsyncOperation.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章