//步骤2:删除旧场景 private void OnDestroyOldScene(string strSceneName, SceneConfig config) { ClearCurScene();//删旧场景 config.sceneName = strSceneName; //StartCoroutine("OnLoadingSceneBegin", config); OnLoadingSceneBegin(config); }
/// <summary> /// 加载场景 /// </summary> /// <param name="strSceneName"></param> public void LoadSceneInBack(string strSceneName) { if (_taskLoadScene != null) { Utils.LogSys.LogWarning("A Scene is loading already!! can't to load a new scene!"); return; } if (!dicSceneCofig.ContainsKey(strSceneName)) { Utils.LogSys.LogWarning("ScenceConfig.xml not find scence: " + strSceneName); return; } if (sCurSenceName == strSceneName) { return; } SceneConfig config = dicSceneCofig[strSceneName]; bool isAddtive = false; switch (config.nOpenType) { case 1: // 切换场景 break; case 2: // 附加场景 isAddtive = true; break; case 3: // 大场景切换(先渐黑,然后切到加载场景,再附加新场景,后删除旧场景) isAddtive = false; break; default: break; } if (config.nType == 1)//本地场景 { _taskLoadScene = new SceneLoadTask(strSceneName, true, isAddtive); } else if (config.nType == 2)//http场景 { _taskLoadScene = new SceneAssetBundleLoadTask(strSceneName, config.sHttp, config.nVersion, isAddtive); } else if (config.nType == 3)//assetbundle场景 { _taskLoadScene = new SceneAssetBundleLoadTask(strSceneName, "", 0, isAddtive); } }
//步骤2-2:加载新场景 void OnLoadingSceneBegin(SceneConfig config) { // yield return new WaitForSeconds(0.5f); LoadSceneInBack(config.sceneName);//加载新场景 _taskLoadScene.EventFinished += new TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { if (!manual)//非被中断 { onLoadComplete(config.sceneName, config); OnAddMonoComplete(config.sceneName, config); } }); _taskLoadScene.ToShowScene(); }
/// <summary> /// 跳转场景 /// </summary> /// <param name="strSceneName"></param> public void ChangeToScene(string strSceneName) { if (IsInBackScene(strSceneName)) { ChageToBackScene(strSceneName); } else { SceneConfig config = dicSceneCofig[strSceneName]; if (config.nOpenType == 2) { ChangeToSceneImpl(strSceneName); } else { LoadingScene.TargetScene = strSceneName; ChangeToSceneImpl(SceneName.s_LoadingScene); } } }
//步骤3:加载新场景的部件 private void OnAddMonoComplete(string strSceneName, SceneConfig config) { if (_sceneMono != null) { _sceneMono.EventLoadUnitsCompete += new BaseScene.LoadUnitsComplete(delegate() { if (onInitComplete != null) { onInitComplete(strSceneName, config); } if (config.nOpenType == 2) { Invoke("OnFadeOutWin", 0.2f); } else if (config.nOpenType == 3) { if (strSceneName != SceneName.s_LoadingScene) { Invoke("OnFadeOutLoadingWin", 0.5f); } } }); _sceneMono.LoadUnits(); } else { if (onInitComplete != null) { onInitComplete(strSceneName, config); } if (config.nOpenType == 2) { Invoke("OnFadeOutWin", 0.2f); } else if (config.nOpenType == 3) { Invoke("OnFadeOutLoadingWin", 0.5f); } } }
//场景加载完成并初始化 public void sceneLoadCompleteAndInit(string strSceneName, SceneConfig config) { _sceneMono = null; if (pCurScene != null) { Destroy(pCurScene); } _taskLoadScene = null; GameObject curScene = GameObject.Find("Scene"); if (pLoadingScene != null) { pCurScene = pLoadingScene; pCurScene.transform.name = "Scene"; pLoadingScene = null; } else { pCurScene = curScene; } sCurSenceName = strSceneName; if (pCurScene == null) { return; } Transform tfWorld = pCurScene.transform.Find("World"); if (tfWorld != null) { tfWorld.gameObject.SetActive(true); } Transform tfCameras = pCurScene.transform.Find("Cameras"); if (tfCameras != null) { tfCameras.gameObject.SetActive(true); } Transform uiCamObj = pCurScene.transform.Find("Cameras/UICamera"); if (uiCamObj) { uiCamObj.gameObject.SetActive(false); } FollowToSceneCamera follower = null; GameObject pListener = GameObject.Find("Listener");//声音接收者 if (pListener == null) { pListener = new GameObject("Listener"); pListener.AddComponent <AudioListener>(); follower = pListener.AddComponent <FollowToSceneCamera>(); DontDestroyOnLoad(pListener); } else { follower = pListener.GetComponent <FollowToSceneCamera>(); } sceneCameraObj = pCurScene.transform.Find("Cameras/SceneCamera").gameObject; if (sceneCameraObj && !sceneCameraObj.GetComponent <CameraAjustor>()) { sceneCameraObj.AddComponent <CameraAjustor>(); follower.camera_tf = sceneCameraObj.transform; AudioListener lisener = sceneCameraObj.GetComponent <AudioListener>(); GameObject.Destroy(lisener); } //音源 if (config.battleScene == 1) { UtilTools.MultiTouchSwitch(true); // _sceneMono = pCurScene.AddComponent<BattleScene>() as BaseScene; } else { BGM csBGM = pCurScene.AddComponent <BGM>(); UtilTools.MultiTouchSwitch(false); switch (strSceneName) { case SceneName.s_StartupScene: _sceneMono = curScene.GetComponent <StartUpScene>() as BaseScene; csBGM.strAudioClipName = "Sounds/BGM/bgm_login_loop"; break; case SceneName.s_LoadingScene: _sceneMono = pCurScene.AddComponent <LoadingScene>(); break; default: _sceneMono = pCurScene.AddComponent <BaseScene>(); break; } // if (strSceneName != SceneName.s_StartupScene && strSceneName != SceneName.s_LoadingScene) // { // AllServerBroadcastController controller_temp = (AllServerBroadcastController)UIManager.GetControler(UIName.ALL_SERVER_BROADCAST); // if (controller_temp != null) // { // controller_temp.ContinuePlay(); // } // } } }
/// <summary> /// 跳到场景 /// </summary> /// <param name="strSceneName"></param> public void ChangeToSceneImpl(string strSceneName) { if (sCurSenceName == strSceneName) { return; } if (!dicSceneCofig.ContainsKey(strSceneName)) { return; } SceneConfig config = dicSceneCofig[strSceneName]; if (config.nOpenType == 1)// 直接切换场景 { ClearBackSceneList(); if (_taskLoadScene == null) { LoadSceneInBack(strSceneName);//加载 _taskLoadScene.ToShowScene(); } //切换完成的回调 _taskLoadScene.EventFinished += new TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { if (!manual)//非被中断 { onLoadComplete(strSceneName, config); OnAddMonoComplete(strSceneName, config);//再加载部件,后渐隐过渡界面 } }); //_taskLoadScene.ToShowScene(); } else if (config.nOpenType == 2)// 附加场景(表现:当前场景渐黑后,显示附加的场景) { if (_taskLoadScene == null) { LoadSceneInBack(strSceneName);//加载场景 } FadeInWinController controller = (FadeInWinController)UIManager.GetControler(UIName.FADE_IN_WIN); //渐黑后的回调 DelegateType.FadeInBlackCallback callback = new DelegateType.FadeInBlackCallback(delegate(string sceneName, SceneConfig scene_config) { _taskLoadScene.ToShowScene(); CurSceneInToBack(); }); //附加完成的回调 _taskLoadScene.EventFinished += new TaskBase.FinishedHandler(delegate(bool manual, TaskBase currentTask) { if (!manual)//非被中断 { onLoadComplete(strSceneName, config); OnAddMonoComplete(strSceneName, config);//再加载部件,后渐隐过渡界面 } }); controller.FadeInBlack(callback); //渐显黑色界面后,触发回调加载场景 } else if (config.nOpenType == 3) // 大场景切换(先渐黑,然后切到加载场景,再附加新场景,后删除加载场景) { if (_taskLoadScene != null && _taskLoadScene._taskName != strSceneName) //当前不是加载场景的要中断 { _taskLoadScene.stop(); _taskLoadScene = null; } LoadingWinController controller = (LoadingWinController)UIManager.GetControler(UIName.LOADING_WIN); //步骤1:先显示云过滤界面 if (strSceneName == SceneName.s_LoadingScene) { controller.FadeInWin(strSceneName, config, OnDestroyOldScene);//渐显界面后,触发回调 } else { OnDestroyOldScene(strSceneName, config); } } }