Example #1
0
    // 加载Loading场景
    IEnumerator LoadLoadingScene(AsyncOperation aon)
    {
        yield return(new WaitForEndOfFrame());

        if (aon.progress < 1)
        {
            StartCoroutine(LoadLoadingScene(aon));
        }
        else
        {
            while (SceneManager.GetActiveScene().name != LoadingScene)
            {
                yield return(null);
            }

            InitProgress();
            // 完成加载Loading场景
            if (_loadingSceneCallback != null)
            {
                _loadingSceneCallback();
                _loadingSceneCallback = null;
            }

            yield return(new WaitForEndOfFrame());

            if (_preLoadObject != null)
            {
                Destroy(_preLoadObject);
            }


            yield return(null);

            AssetBundleManager.Instance.ClearBundleMap();
                        #if !UNITY_ANDROID || UNITY_EDITOR
            AssetBundleManager.Instance.ClearAudioMap();
                        #endif

            Resources.UnloadUnusedAssets();
            System.GC.Collect();

            if (!string.IsNullOrEmpty(_bundleName))
            {
                var prefab = ResourceLoadUtils.Load <GameObject>("SceneInitObjects/" + _bundleName);
                if (prefab)
                {
                    GameObject obj = Instantiate(prefab);
                    obj.name = obj.name.Replace("(Clone)", "");
                    DontDestroyOnLoad(obj);
                    _preLoadObject = obj;
                }
            }

            StartLoadNextScene();
        }
    }
    protected IEnumerator WaitForLoadScenePassEmpty(string targetScene,
                                                    WaitLoadSceneCallback waitLoadSceneCallback = null, LoadingSceneCallback loadingSceneCallback = null, LoadedSceneCallback loadedSceneCallback = null)
    {
        this.waitLoadSceneCallback = waitLoadSceneCallback;
        this.loadingSceneCallback  = loadingSceneCallback;
        this.loadedSceneCallback   = loadedSceneCallback;

        yield return(null);

        if (LoadingScene != targetScene)
        {
            LoadingScene = targetScene;
            //异步等待加载空场景
            yield return(DynaLoadScene(ESceneType.EmptyScene.ToString()));

            mAsync = SceneManager.LoadSceneAsync(ESceneType.EmptyScene.ToString());

            yield return(mAsync);

            yield return(CSAssist.waitForEndOfFrame);

            //如果存在开始加载真正场景之前的操作,那么在回调返回true之前,不进行下一步
            if (waitLoadSceneCallback != null)
            {
                while (waitLoadSceneCallback() == false)
                {
                    yield return(CSAssist.waitForEndOfFrame);
                }
            }

            //开始加载真正需要的场景
            mAsync = SceneManager.LoadSceneAsync(targetScene.ToString());
            mAsync.allowSceneActivation = false;

            yield return(null);

            while (!mAsync.isDone && mAsync.allowSceneActivation == false)
            {
                if (mAsync.progress < 0.9f)
                {
                    if (loadingSceneCallback != null)
                    {
                        loadingSceneCallback(mAsync.progress);
                    }
                    yield return(null);
                }
                else
                {
                    mAsync.allowSceneActivation = true;
                    break;
                }
            }
            yield return(mAsync);

            if (mAsync.isDone)
            {
                if (loadedSceneCallback != null)
                {
                    loadedSceneCallback();
                }
            }
        }
        else
        {
            if (CSDebug.developerConsoleVisible)
            {
                CSDebug.LogError("you try to load the same Scene = " + LoadingScene);
            }
        }
    }
 public void LoadByName(string sceneName,
                        WaitLoadSceneCallback waitLoadSceneCallback = null, LoadingSceneCallback loadingSceneCallback = null, LoadedSceneCallback loadedSceneCallback = null)
 {
     StartCoroutine(WaitForLoadScenePassEmpty(sceneName, waitLoadSceneCallback, loadingSceneCallback, loadedSceneCallback));
 }
 public void Load(ESceneType targetScene,
                  WaitLoadSceneCallback waitLoadSceneCallback = null, LoadingSceneCallback loadingSceneCallback = null, LoadedSceneCallback loadedSceneCallback = null)
 {
     LoadByName(targetScene.ToString(), waitLoadSceneCallback, loadingSceneCallback, loadedSceneCallback);
 }