/// <summary> /// Load scenes in bundle /// </summary> private static void LoadScenes(SceneBundle bundle, bool hasToFullyReload, int persistantScenesCount) { //Get scenes in the bundle var scenes = bundle.GetScenes(); SceneManager.LoadScene(scenes[0], hasToFullyReload ? LoadSceneMode.Single : LoadSceneMode.Additive); setActiveScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); //Get the loaded scene to set it as active later for (int i = 1; i < scenes.Length; i++) { SceneManager.LoadScene(scenes[i], LoadSceneMode.Additive); } if (persistantScenesCount >= 0) { //Check if persistant scenes are correctly loaded if (persistantScenesCount != currentSceneList.PersistantScenesBundle.ScenesCount) { //Re-open persistant scenes for (int i = 0; i < currentSceneList.PersistantScenesBundle.ScenesCount; i++) { SceneManager.LoadScene(currentSceneList.PersistantScenesBundle.GetSceneAtID(i), LoadSceneMode.Additive); } } } }
/// <summary> /// Async loading coroutine /// </summary> /// <param name="bundle"></param> /// <returns></returns> private static IEnumerator LoadingAsyncCoroutine(SceneBundle bundle, bool hasToFullyReload, int persistantScenesCount) { //Get scenes in the bundle var scenes = bundle.GetScenes(); AsyncOperation loadingOp; loadingOp = SceneManager.LoadSceneAsync(scenes[0], hasToFullyReload ? LoadSceneMode.Single : LoadSceneMode.Additive); yield return(loadingOp); setActiveScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); //Get the loaded scene to set it as active later for (int i = 1; i < scenes.Length; i++) { loadingOp = SceneManager.LoadSceneAsync(scenes[i], LoadSceneMode.Additive); yield return(loadingOp); } if (persistantScenesCount >= 0) { //Check if persistant scenes are correctly loaded if (persistantScenesCount != currentSceneList.PersistantScenesBundle.ScenesCount) { //Re-open persistant scenes for (int i = 0; i < currentSceneList.PersistantScenesBundle.ScenesCount; i++) { loadingOp = SceneManager.LoadSceneAsync(currentSceneList.PersistantScenesBundle.GetSceneAtID(i), LoadSceneMode.Additive); yield return(loadingOp); } } } yield return(null); }
/// <summary> /// The loading process coroutine /// </summary> /// <param name="loadAsync"></param> /// <returns></returns> private static IEnumerator LoadingProcessCoroutine(SceneBundle bundle, bool loadAsync) { List <Scene> scenesToUnload = GetScenesToUnload(out bool hasToFullyReload, out int persistantScenesCount); yield return(UnloadingAsyncCoroutine(scenesToUnload, hasToFullyReload)); //Load scenes isLoading = true; if (loadAsync) { yield return(LoadingAsyncCoroutine(bundle, hasToFullyReload, persistantScenesCount)); } else { LoadScenes(bundle, hasToFullyReload, persistantScenesCount); } //Has to wait for a complete frame yield return(null); isLoading = false; SceneManager.SetActiveScene(setActiveScene); currentSceneBundle = bundle; onSceneAllLoaded?.Invoke(); sceneOrchestrator.StopLoadingCoroutine(); //Stops the coroutine handler yield return(null); }
/// <summary> /// Check if the bundle can be loaded /// </summary> /// <returns></returns> private static bool CanLoad(SceneBundle targetBundle) { //Return false if trying to load a bundle that doesn't belong to the current scene list if (!currentSceneList.HasBundleInList(targetBundle)) { Debug.LogError("This scene bundle doesn't belong to the scene list! It cannot be loaded"); return(false); } //Return false if trying to load persistant scene bundle if (targetBundle == currentSceneList.PersistantScenesBundle) { Debug.LogError("Persistant scene bundle is automaticaly and permanently loaded, you cannot load it manually"); return(false); } //Return false if a bundle is currently loading if (isLoading) { Debug.LogError("Cannot load another bundle while the Enhanced Scene Manager is loading scenes"); return(false); } //Return false if a bundle is currently unloading if (isUnloading) { Debug.LogError("Cannot load another bundle while the Enhanced Scene Manager is unloading scenes"); return(false); } return(true); }
/// <summary> /// Check if the input bundle is contained in the level list /// </summary> public bool HasBundleInList(SceneBundle bundleToCheck) { foreach (SceneBundle bundle in scenesBundles) { if (bundleToCheck == bundle) { return(true); } } return(false); }
/// <summary> /// Load a new scene bundle and unload the current one /// </summary> /// <param name="groupName">The target bundle</param> public static void LoadSceneBundle(SceneBundle bundle, bool async = false) { if (!CanLoad(bundle)) { return; } onTriggerLoading?.Invoke(); if (sceneOrchestrator == null) { InstantiateSceneOrchestrator(); } sceneOrchestrator.StartLoadingCoroutine(LoadingProcessCoroutine(bundle, async)); //Call a start coroutine on the scene oorchestrator }