Esempio n. 1
0
    public IEnumerator LoadScene(string nextScene)
    {
        Scene curScene = SceneManager.GetActiveScene();

        if (string.IsNullOrEmpty(nextScene) || m_isLoading || curScene.name.Equals(nextScene))
        {
            yield break;
        }

        m_isLoading = true;

        SceneManager.LoadScene(SceneConst.SceneName, LoadSceneMode.Additive);

        yield return(StartCoroutine(AsyncLoadScene(nextScene)));

        FreshProcessEvent.Invoke(SceneConst.LOADSCENEPROCESS);
        m_unloadAsync = SceneManager.UnloadSceneAsync(curScene);

        /*
         * //使用LoadSceneMode.Additive进行场景切换,无法完全释放资源。即使使用Resources.UnloadUnusedAssets()也不行
         * //可能出现如下情况:
         * //1.资源没有引用了,但是还没有回收
         * //2.被UnityEngine.EventSystems.StandaloneInputModule和UnityEngine.EventSystems引用
         * //当然如果单纯从一个场景切换另外一个场景,直接使用UnityEngine.SceneManagement.SceneManager.LoadScene("xxx")
         * //直接切换场景,这个会进行内存回收
         */
        yield return(m_unloadAsync);

        FreshProcessEvent.Invoke(SceneConst.UNSCENEPROCESS);
        //场景中必须有一个对象挂载SceneBase
        SceneBase sceneLoader = UnityTool.GetTopObject("SceneLoader").GetComponent <SceneBase>();

        if (sceneLoader == null)
        {
            Debug.LogError("lack of gameobject of name 'SceneLoader'");
            yield break;
        }

        yield return(new WaitUntil(sceneLoader.FinishConfigRes));

        FreshProcessEvent.Invoke(SceneConst.INITNETPROCESS);

        yield return(new WaitUntil(sceneLoader.FinishGetData));

        FreshProcessEvent.Invoke(SceneConst.INITCONFIGPROCESS);

        yield return(new WaitUntil(sceneLoader.FinishInit));

        FreshProcessEvent.Invoke(SceneConst.INITSCENEPROCESS);

        yield return(new WaitForSeconds(1f));

        FreshProcessEvent.Invoke(1f);
        m_loadAsync = SceneManager.UnloadSceneAsync(SceneConst.SceneName);

        yield return(m_loadAsync);

        m_isLoading = false;
    }
Esempio n. 2
0
        public IEnumerator LoadScene(string nextScene)
        {
            Scene curScene = SceneManager.GetActiveScene();

            if (string.IsNullOrEmpty(nextScene) || isLoading || curScene.name.Equals(nextScene))
            {
                yield break;
            }

            isLoading = true;

            SceneManager.LoadScene("LoadingScene", LoadSceneMode.Additive);

            yield return(StartCoroutine(AsyncLoadScene(nextScene)));

            FreshProcessEvent.Invoke(SceneConst.LOADSCENEPROCESS);
            unloadAsync = SceneManager.UnloadSceneAsync(curScene);

            //2017版本,如果切换场景不进行Resources.UnloadUnusedAssets(),尽快回收资源,但是材质所引用的贴图可能无法切换一次场景
            //回收,它仍然被ugui的事件UnityEngine.EventSystems.StandaloneInputModule和UnityEngine.EventSystems引用
            //也可能只是被材质引用
            //   UnityEngine.SceneManagement.SceneManager.LoadScene("xxx")直接切换场景时,上面的问题不存在
            yield return(unloadAsync);

            Resources.UnloadUnusedAssets();
            FreshProcessEvent.Invoke(SceneConst.UNSCENEPROCESS);
            SceneBase sceneLoader = UnityTool.GetTopObject("SceneLoader").GetComponent <SceneBase>();

            yield return(new WaitUntil(sceneLoader.FinishConfigRes));

            FreshProcessEvent.Invoke(SceneConst.INITNETPROCESS);

            yield return(new WaitUntil(sceneLoader.FinishGetData));

            FreshProcessEvent.Invoke(SceneConst.INITCONFIGPROCESS);

            yield return(new WaitUntil(sceneLoader.FinishInit));

            FreshProcessEvent.Invoke(SceneConst.INITSCENEPROCESS);

            yield return(new WaitForSeconds(1f));

            FreshProcessEvent.Invoke(1f);
            unloadAsync = SceneManager.UnloadSceneAsync("LoadingScene");

            yield return(unloadAsync);

            isLoading = false;
        }