public IEnumerator Load(string[] targetUnloadScenes, string[] targetLoadScenes, Action onLoadComplete) { SceneManager.LoadScene(LoadingScene, LoadSceneMode.Additive); // Wait for the end of the current frame to finish to ensure that everything has cleaned up. yield return(new WaitForEndOfFrame()); for (int i = 0; i < targetUnloadScenes.Length; i++) { SceneManager.UnloadScene(targetUnloadScenes[i]); } int scenesLoaded = 0; float totalProgress = 0f; foreach (var scene in targetLoadScenes) { var task = SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive); while (!task.isDone) { totalProgress = (scenesLoaded + task.progress) / targetLoadScenes.Length; LoadProgress.InvokeSafe(totalProgress); yield return(null); } scenesLoaded++; } SceneManager.UnloadScene(LoadingScene); // We're going to assume that the last scene is what we want to be active... var activeScene = SceneManager.GetSceneByName(targetLoadScenes[targetLoadScenes.Length - 1]); SceneManager.SetActiveScene(activeScene); onLoadComplete.InvokeSafe(); }
private void CommenceLoading(SceneReference targetScene) { FadeToBlack().Then(() => { SceneManager.SetActiveScene(SceneManager.GetSceneByPath(BaseScene.SceneName)); }).Then(() => { gameObject.BroadcastToAll("PreOnLoadingScreenOn"); }).Then( () => LoadScene(LoadingScreen) ).Then(() => { gameObject.BroadcastToAll("PostOnLoadingScreenOn"); }).Then( UnfadeFromBlack ).Then( UnloadStrayScenes ).Then( () => LoadScene(targetScene) ).Then(() => { gameObject.BroadcastToAll("PreOnLoadingScreenOff"); }).Then( FadeToBlack ).Then( () => UnloadScene(LoadingScreen) ).Then(() => { gameObject.BroadcastToAll("PostOnLoadingScreenOff"); SceneManager.SetActiveScene(SceneManager.GetSceneByPath(targetScene.SceneName)); }).Then( UnfadeFromBlack ).Then ( () => EndLoadScene() ).Catch(exception => { Debug.LogError($"Scene loading failed with following exception: {exception.ToString()}"); }); }