// Used for switching between levels. IEnumerator UnloadAndLoadAsync(SceneLoadInfo sceneToUnload, SceneLoadInfo sceneToLoad) { List <AsyncOperation> toWaitFor = new List <AsyncOperation>(); // Ensure we only unload scenes that we won't be using in the next scene. foreach (string sceneName in currentlyLoaded.sceneDefinition.ObjectReference.AllScenes) { if (sceneToLoad.sceneDefinition.ObjectReference.AllScenes.Where(item => item == sceneName).Count() == 0) { DevTools.Logger.Log(SceneServiceLog, "Unloading {}", () => new object[] { sceneName }); toWaitFor.Add(SceneManager.UnloadSceneAsync(sceneName)); } } foreach (AsyncOperation asyncLevelUnload in toWaitFor) { yield return(asyncLevelUnload); } System.GC.Collect(); OnUnload(); sceneObjectsOfCategory.Clear(); DevTools.Logger.Log(SceneServiceLog, "Finished Unloading"); yield return(LoadActiveAsync(sceneToLoad, LoadSceneMode.Additive)); }
// Used for loading next level. IEnumerator LoadActiveAsync(SceneLoadInfo sceneToLoad, LoadSceneMode loadSceneMode) { List <AsyncOperation> toWaitFor = new List <AsyncOperation>(); foreach (string sceneName in sceneToLoad.sceneDefinition.ObjectReference.AllScenes) { if (!SceneManager.GetSceneByName(sceneName).isLoaded) { DevTools.Logger.Log(SceneServiceLog, "Loading {}", () => new object[] { sceneName }); toWaitFor.Add(SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive)); } } foreach (AsyncOperation asyncLevelUnload in toWaitFor) { yield return(asyncLevelUnload); } Scene primaryScene = SceneManager.GetSceneByName(sceneToLoad.sceneDefinition.ObjectReference.PrimaryScene); SceneManager.SetActiveScene(primaryScene); currentlyLoaded = sceneToLoad; // Use to change the state of the active scenes GameObjects before progressing (Loading scene save data ect.) OnPreFinishedLoading(); currentState = SceneLoadingState.Loaded; DevTools.Logger.Log(SceneServiceLog, "Finished Loading Scenes"); OnFinishedLoading(sceneToLoad); }
private void SaveService_OnPreLoad(CoroutineEvent coroutineEvent) { SceneLoadInfo sceneLoadInfo = ServiceLocator.SaveService.GetSaveData <SceneLoadInfo>(Animator.StringToHash("PreviousScene")); sceneLoadInfo.fromSave = true; coroutineEvent.coroutines.Add(LoadScene(sceneLoadInfo)); }
public Coroutine LoadScene(SceneLoadInfo sceneToLoad) { currentState = SceneLoadingState.Loading; OnLoadScene(sceneToLoad); if (currentlyLoaded == null) { return(owner.StartCoroutine(LoadActiveAsync(sceneToLoad, LoadSceneMode.Single))); } return(owner.StartCoroutine(UnloadAndLoadAsync(currentlyLoaded, sceneToLoad))); }
public Coroutine LoadScene(SceneLoadInfo sceneToLoad) { return(null); }