//切换场景 public static async ETTask SwitchScene(this SceneManagerComponent self, SceneConfig scene_config, bool needclean = false, SceneLoadComponent slc = null) { if (self.Busing) { return; } if (scene_config == null) { return; } if (self.CurrentScene == scene_config.Name) { return; } self.Busing = true; //打开loading界面 Log.Info("InnerSwitchScene start open uiloading"); await Game.EventSystem.PublishAsync(new UIEventType.LoadingBegin()); await self.InnerSwitchScene(scene_config, needclean, slc); //加载完成,关闭loading界面 await Game.EventSystem.PublishAsync(new UIEventType.LoadingFinish()); //释放loading界面引用的资源 GameObjectPoolComponent.Instance.CleanupWithPathArray(true, self.ScenesChangeIgnoreClean); self.Busing = false; }
Dictionary <string, SceneConfig> GetSceneConfig() { SceneConfig LoadingScene = new SceneConfig { SceneAddress = "Scenes/LoadingScene/Loading.unity", Name = SceneNames.Loading, }; SceneConfig LoginScene = new SceneConfig { SceneAddress = "Scenes/LoginScene/Login.unity", Name = SceneNames.Login, }; SceneConfig MapScene = new SceneConfig { SceneAddress = "Scenes/MapScene/Map.unity", Name = SceneNames.Map, }; var res = new Dictionary <string, SceneConfig>(); res.Add(LoadingScene.Name, LoadingScene); res.Add(LoginScene.Name, LoginScene); res.Add(MapScene.Name, MapScene); return(res); }
//切换场景 async static ETTask InnerSwitchScene(this SceneManagerComponent self, SceneConfig scene_config, bool needclean = false, SceneLoadComponent slc = null) { float slid_value = 0; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); CameraManagerComponent.Instance.SetCameraStackAtLoadingStart(); //等待资源管理器加载任务结束,否则很多Unity版本在切场景时会有异常,甚至在真机上crash Log.Info("InnerSwitchScene ProsessRunning Done "); while (ResourcesComponent.Instance.IsProsessRunning()) { await TimerComponent.Instance.WaitAsync(1); } slid_value += 0.01f; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); await TimerComponent.Instance.WaitAsync(1); //清理UI Log.Info("InnerSwitchScene Clean UI"); await UIManagerComponent.Instance.DestroyWindowExceptNames(self.DestroyWindowExceptNames.ToArray()); slid_value += 0.01f; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); //清除ImageLoaderManager里的资源缓存 这里考虑到我们是单场景 Log.Info("InnerSwitchScene ImageLoaderManager Cleanup"); ImageLoaderComponent.Instance.Clear(); //清除预设以及其创建出来的gameobject, 这里不能清除loading的资源 Log.Info("InnerSwitchScene GameObjectPool Cleanup"); if (needclean) { GameObjectPoolComponent.Instance.Cleanup(true, self.ScenesChangeIgnoreClean); slid_value += 0.01f; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); //清除除loading外的资源缓存 List <UnityEngine.Object> gos = new List <UnityEngine.Object>(); for (int i = 0; i < self.ScenesChangeIgnoreClean.Count; i++) { var path = self.ScenesChangeIgnoreClean[i]; var go = GameObjectPoolComponent.Instance.GetCachedGoWithPath(path); if (go != null) { gos.Add(go); } } Log.Info("InnerSwitchScene ResourcesManager ClearAssetsCache excludeAssetLen = " + gos.Count); ResourcesComponent.Instance.ClearAssetsCache(gos.ToArray()); slid_value += 0.01f; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); } else { slid_value += 0.02f; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); } await ResourcesComponent.Instance.LoadSceneAsync(self.GetSceneConfigByName(SceneNames.Loading).SceneAddress, false); Log.Info("LoadSceneAsync Over"); slid_value += 0.01f; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); //GC:交替重复2次,清干净一点 GC.Collect(); GC.Collect(); var res = Resources.UnloadUnusedAssets(); while (!res.isDone) { await TimerComponent.Instance.WaitAsync(1); } slid_value += 0.12f; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); Log.Info("异步加载目标场景 Start"); //异步加载目标场景 await ResourcesComponent.Instance.LoadSceneAsync(scene_config.SceneAddress, false); slid_value += 0.65f; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); //准备工作:预加载资源等 if (slc != null) { await slc.OnPrepare((progress) => { Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value + 0.15f * progress }); if (progress > 1) { Log.Error("scene load waht's the f**k!"); } }); } slid_value += 0.15f; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); CameraManagerComponent.Instance.SetCameraStackAtLoadingDone(); self.CurrentScene = scene_config.Name; slid_value = 1; Game.EventSystem.Publish(new UIEventType.LoadingProgress { Progress = slid_value }); //等久点,跳的太快 await TimerComponent.Instance.WaitAsync(500); }
public static bool IsInTargetScene(this SceneManagerComponent self, SceneConfig scene_config) { return(self.CurrentScene == scene_config.Name); }