Beispiel #1
0
    //------------------------------------------------------------------------------------------------------------------------------
    protected IEnumerator loadSceneCoroutine(SceneInstance scene, bool active, SceneLoadCallback callback, object userData = null)
    {
        // 所有场景都只能使用叠加的方式来加载,方便场景管理器来管理所有场景的加载和卸载
        scene.mOperation = SceneManager.LoadSceneAsync(scene.mName, LoadSceneMode.Additive);
        // allowSceneActivation指定了加载场景时是否需要调用场景中所有脚本的Awake和Start,以及贴图材质的引用等等
        scene.mOperation.allowSceneActivation = true;
        while (true)
        {
            if (callback != null)
            {
                callback(scene.mOperation.progress, false, userData);
            }
            // 当allowSceneActivation为true时,加载到progress为1时停止,并且isDone为true,scene.isLoaded为true
            // 当allowSceneActivation为false时,加载到progress为0.9时就停止,并且isDone为false, scene.isLoaded为false
            // 当场景被激活时isDone变为true,progress也为1,scene.isLoaded为true
            if (scene.mOperation.isDone || scene.mOperation.progress >= 1.0f)
            {
                break;
            }

            yield return(null);
        }
        // 首先获得场景
        scene.mScene = SceneManager.GetSceneByName(scene.mName);
        // 获得了场景根节点才能使场景显示或隐藏
        scene.mRoot = UnityUtility.getGameObject(null, scene.mName + "_Root", true);
        activeScene(scene.mName, active);
        scene.mState = LOAD_STATE.LS_LOADED;
        if (callback != null)
        {
            callback(1.0f, true, userData);
        }
    }
Beispiel #2
0
 public override void update(float elapsedTime)
 {
     base.update(elapsedTime);
     if (mDirectLoadCallback.Count > 0)
     {
         // 只调用列表中第一个回调,避免回调中再次对该列表进行修改,且需要在列表中移除后再调用
         SceneLoadCallback callback = mDirectLoadCallback[0];
         mDirectLoadCallback.RemoveAt(0);
         callback(1.0f, true);
     }
     // 场景AssetBundle加载完毕时才开始加载场景
     for (int i = 0; i < mLoadList.Count; ++i)
     {
         if (mLoadList[i].mState == LOAD_STATE.LS_UNLOAD)
         {
             mGameFramework.StartCoroutine(loadSceneCoroutine(mLoadList[i]));
         }
         else if (mLoadList[i].mState == LOAD_STATE.LS_LOADED)
         {
             mLoadList.RemoveAt(i--);
         }
     }
     foreach (var item in mSceneList)
     {
         if (item.Value.getActive())
         {
             item.Value.update(elapsedTime);
         }
     }
 }
Beispiel #3
0
    public IEnumerator FadeCoroutine(float waitTime, SceneLoadCallback sceneCallback)
    {
        yield return(new WaitForSecondsRealtime(waitTime));

        if (!isFading)
        {
            yield return(FadeScenCoroutine(faderBlack, fadeOut));

            sceneCallback();

            yield return(FadeScenCoroutine(faderBlack, fadeIn));
        }
    }
Beispiel #4
0
    // 目前只支持异步加载,因为SceneManager.LoadScene并不是真正地同步加载
    // 该方法只能保证在这一帧结束后场景能加载完毕,但是函数返回后场景并没有加载完毕
    public void loadSceneAsync(string name, bool active, SceneLoadCallback callback, object userData = null)
    {
        // 如果场景已经加载,则直接返回
        if (mSceneList.ContainsKey(name))
        {
            if (callback != null)
            {
                callback(1.0f, true, userData);
            }
            return;
        }
        SceneInstance scene = createScene(name);

        scene.mState = LOAD_STATE.LS_LOADING;
        mSceneList.Add(scene.mName, scene);
        mGameFramework.StartCoroutine(loadSceneCoroutine(scene, active, callback, userData));
    }
Beispiel #5
0
    // 目前只支持异步加载,因为SceneManager.LoadScene并不是真正地同步加载
    // 该方法只能保证在这一帧结束后场景能加载完毕,但是函数返回后场景并没有加载完毕
    public void loadSceneAsync(string sceneName, bool active, SceneLoadCallback callback)
    {
        // 如果场景已经加载,则直接返回
        if (mSceneList.ContainsKey(sceneName))
        {
            showScene(sceneName);
            if (callback != null)
            {
                mDirectLoadCallback.Add(callback);
            }
            return;
        }
        SceneInstance scene = createScene(sceneName);

        scene.mState        = LOAD_STATE.LS_UNLOAD;
        scene.mActiveLoaded = active;
        scene.mLoadCallback = callback;
        mSceneList.Add(scene.mName, scene);
        // scenePath + sceneName表示场景文件AssetBundle的路径,包含文件名
        mResourceManager.loadAssetBundleAsync(getScenePath(sceneName) + sceneName, onSceneAssetBundleLoaded, scene);
    }
Beispiel #6
0
    //------------------------------------------------------------------------------------------------------------------------------
    protected IEnumerator loadSceneCoroutine(string name, LoadSceneMode mode, bool active, SceneLoadCallback callback, object userData)
    {
        SceneInstance scene = new SceneInstance();

        scene.mState     = LOAD_STATE.LS_LOADING;
        scene.mOperation = SceneManager.LoadSceneAsync(name, mode);
        scene.mOperation.allowSceneActivation = true;
        mSceneList.Add(name, scene);
        while (true)
        {
            if (callback != null)
            {
                callback(scene.mOperation, false, userData);
            }
            // 当allowSceneActivation为true时,加载到progress为1时停止,并且isDone为true
            // 当allowSceneActivation为false时,加载到progress为0.9时就停止,并且isDone为false,当场景被激活时isDone变为true,progress也为1
            if (scene.mOperation.isDone && scene.mOperation.progress >= 1.0f)
            {
                break;
            }
            yield return(null);
        }
        scene.mRoot = UnityUtility.getGameObject(null, name + "_Root");
        if (scene.mRoot == null)
        {
            UnityUtility.logError(name + " scene must have a root node with name : " + name + "_Root");
        }
        activeScene(name, active);
        scene.mState = LOAD_STATE.LS_LOADED;
        scene.mScene = SceneManager.GetSceneByName(name);
        if (callback != null)
        {
            callback(scene.mOperation, true, userData);
        }
    }
Beispiel #7
0
 public void loadSceneAsync(string name, LoadSceneMode mode, bool active, SceneLoadCallback callback, object userData = null)
 {
     StartCoroutine(loadSceneCoroutine(name, mode, active, callback, userData));
 }