private IEnumerator LoadSceneAsyncCoroutine(SceneLoadAsyncPlan loadPlan) { var asyncOperation = SceneManager.LoadSceneAsync(loadPlan.sceneName, loadPlan.loadSceneMode); loadPlan.LoadStart(asyncOperation); //更新状态 asyncOperation.allowSceneActivation = loadPlan.allowSceneActivation; while (!asyncOperation.isDone) { loadPlan.InvokeProgress(asyncOperation.progress); if (!loadPlan.allowSceneActivation && asyncOperation.progress >= 0.9f) { //这时候异步加载的工作已经完成了(虽然还没加载完),要把OnFinish回调出去 loadPlan.InvokeFinishIfNotActivation(); } yield return(null); } if (asyncOperation.isDone) { loadPlan.InvokeFinishOnTrueDone(); //从计划列表中拿掉自己 if (mAsyncLoadPlanPool.Contains(loadPlan)) { mAsyncLoadPlanPool.Remove(loadPlan); } yield break; } }
//private async UniTask OpenSceneAsyncWithCallback(string scene_name, Action finishCallback, LoadSceneMode loadSceneMode = LoadSceneMode.Single) //{ // await SceneManager.LoadSceneAsync(scene_name, loadSceneMode); // if (finishCallback != null) // { // finishCallback(); // } //} /// <summary> /// /// </summary> /// <param name="scene_name">场景名</param> /// <param name="loadSceneMode">加载模式</param> /// <param name="allowSceneActivation">允许在场景准备就绪后立即激活场景。</param> /// <param name="onFinishCallback">加载结束回调</param> /// <param name="onProgressCallback">加载过程进度回调</param> private void OpenSceneAsyncWithRxCoroutine(string scene_name, LoadSceneMode loadSceneMode = LoadSceneMode.Single, bool allowSceneActivation = true, Action <SceneLoadAsyncPlan> onFinishCallback = null, Action <float> onProgressCallback = null) { //判断是否已经在加载 var flag = false; for (int i = 0; i < mAsyncLoadPlanPool.Count; i++) { if (mAsyncLoadPlanPool[i].sceneName == scene_name) { flag = true; //欸呀呀,已经正在加载它了呀w(゚Д゚)w if (onFinishCallback != null) { mAsyncLoadPlanPool[i].AddFinishCallback(onFinishCallback); } //以callback之名起誓:等它加载好了顺便告诉我一声哦 if (onProgressCallback != null) { mAsyncLoadPlanPool[i].AddLoadProgressCallback(onProgressCallback); } //ね ね , 顺便把加载进度告诉我呗 break; } } if (!flag) { //创建一个计划 var loadPlan = new SceneLoadAsyncPlan( _scene_name: scene_name, _loadSceneMode: loadSceneMode, _allowSceneActivation: allowSceneActivation, _onFinishCallback: onFinishCallback, _onProgressCallback: onProgressCallback ); //把计划加入到pool if (!mAsyncLoadPlanPool.Contains(loadPlan)) { mAsyncLoadPlanPool.Add(loadPlan); } //启动一个协程去加载它呀 MainThreadDispatcher.StartUpdateMicroCoroutine(LoadSceneAsyncCoroutine(loadPlan)); } }