/// <summary> /// 加载场景资源 (优先加载AssetBundle 资源) (不要直接使用字符串场景名) /// </summary> /// <param name="sceneNameEnum"></param> /// <param name="isSingleLoad">加载场景模式 (true 标识加载完成删除其他的场景资源 否则 保留其他的场景资源 )</param> /// <param name="callback">=true 标识场景加载成功 否则失败</param> public void LoadSceneAsync(SceneNameEnum sceneNameEnum, LoadSceneModeEnum loadMode, System.Action <bool> callback, List <SceneNameEnum> ignoreUnLoadScene, System.Action onCompleteUnLoadSceneAct = null) { if (IsUnLoadingScene) { Debug.LogError("卸载场景时候加载会有异常 请先确认判断有没有完成资源卸载 然后处理加载逻辑"); return; } string sceneName = sceneNameEnum.ToString(); if (string.IsNullOrEmpty(sceneName)) { Debug.LogError("加载场景失败 ,未知的场景资源名称" + sceneName); if (callback != null) { callback(false); } return; } int index = CheckIfSceneLoaded(sceneName); if (index != -1) { Debug.LogError("The Scene Is Already Loaded " + sceneName); Scene scene = m_CurLoadedScene[index]; m_CurLoadedScene.RemoveAt(index); m_CurLoadedScene.Add(scene); //更新当前加载的场景到最后面 if (callback != null) { callback(true); } return; } DealWithLoadMode(loadMode, ignoreUnLoadScene, () => { if (onCompleteUnLoadSceneAct != null) { onCompleteUnLoadSceneAct.Invoke(); } ResourcesLoader.SceneLoader.LoadScene(S_SceneDirectory + sceneName, LoadAssetModel.Async, (loader) => { if (loader == null || loader.IsCompleted == false || loader.IsError) { Debug.LogError("场景资源加载失败"); if (callback != null) { callback(false); } return; } EventCenter.Instance.StartCoroutine(InstantiateScene(sceneName, LoadSceneMode.Additive, callback)); }); }); //先卸载资源然后 再加载新的场景 }
/// <summary> /// 加载完成时候 卸载其他的场景资源 /// </summary> /// <param name="loadMode"></param> /// <param name="ignoreUnLoadScene"></param> private void DealWithLoadMode(LoadSceneModeEnum loadMode, List <SceneNameEnum> ignoreUnLoadScene, System.Action onCompleteUnLoadAct) { if (ignoreUnLoadScene == null) { ignoreUnLoadScene = new List <SceneNameEnum>(); } List <Scene> allNeedUnLoadScene = new List <Scene>(); //需要卸载的场景 List <int> allNeedRemoveSceneIndex = new List <int>(); SceneNameEnum sceneName = SceneNameEnum.ApplicationEntry; #region 根据不同的加载模式获取需要卸载的场景列表 switch (loadMode) { case LoadSceneModeEnum.ReleasAll: for (int dex = 0; dex < m_CurLoadedScene.Count; ++dex) { sceneName = (SceneNameEnum)System.Enum.Parse(typeof(SceneNameEnum), m_CurLoadedScene[dex].name); if (ignoreUnLoadScene.Contains(sceneName) == false && S_AllIgnoreUnLoadScene.Contains(sceneName) == false) { allNeedRemoveSceneIndex.Add(dex); allNeedUnLoadScene.Add(m_CurLoadedScene[dex]); } } break; case LoadSceneModeEnum.ReleaseSelf: if (m_CurLoadedScene.Count > 0) { Debug.Log("m_CurLoadedScene " + m_CurLoadedScene.Count); sceneName = (SceneNameEnum)System.Enum.Parse(typeof(SceneNameEnum), m_CurLoadedScene[m_CurLoadedScene.Count - 1].name); if (ignoreUnLoadScene.Contains(sceneName) == false && S_AllIgnoreUnLoadScene.Contains(sceneName) == false) { allNeedRemoveSceneIndex.Add(m_CurLoadedScene.Count - 1); allNeedUnLoadScene.Add(m_CurLoadedScene[m_CurLoadedScene.Count - 1]); } } break; case LoadSceneModeEnum.KeepPrevious: break; default: Debug.LogEditorInfor("没有定义的加载场景类型 " + loadMode); break; } #endregion allNeedRemoveSceneIndex.Reverse(); for (int dex = 0; dex < allNeedRemoveSceneIndex.Count; ++dex) { m_CurLoadedScene.RemoveAt(allNeedRemoveSceneIndex[dex]); } EventCenter.Instance.StartCoroutine(UnLoadScenes(allNeedUnLoadScene, onCompleteUnLoadAct)); }
/// <summary> /// 加载场景资源 (优先加载AssetBundle 资源) (不要直接使用字符串场景名) /// </summary> /// <param name="sceneNameEnum"></param> /// <param name="callback"></param> public void LoadSceneAsync(SceneNameEnum sceneNameEnum, LoadSceneModeEnum loadMode, System.Action <bool> callback, System.Action onCompleteUnLoadSceneAct = null) { LoadSceneAsync(sceneNameEnum, loadMode, callback, new List <SceneNameEnum>(), onCompleteUnLoadSceneAct); }