public static void LoadAdditiveSceneAsync(SceneLoadArg arg) { if (!arg.IsValid) { Debug.LogError("Scene operation cancelled."); return; } if (!IsLoading) { GameObject gameObj = new GameObject("Scene Progress"); gameObj.AddComponent <SceneLoader>(); SourceScene = gameObj.scene.path; DontDestroyOnLoad(gameObj); } sceneOperationQueue.Enqueue(Instance.LoadAdditiveSceneAsyncCoroutine(arg)); }
private IEnumerator LoadSceneAsyncCoroutine(SceneLoadArg arg) { List <string> scenePathsToUnload = new List <string>(); for (int i = 0; i < SceneManager.sceneCount; i++) { Scene scene = SceneManager.GetSceneAt(i); if (scene.isLoaded) { scenePathsToUnload.Add(scene.path); } } Scene tempScene = SceneManager.CreateScene("__Temp__"); SetActiveScene(tempScene); if (arg.HasTransitionScene) { yield return(StartCoroutine(LoadAdditiveCoroutine(arg.TransitionScenePath))); } yield return(StartCoroutine(UnloadSceneAsyncCoroutine(false, scenePathsToUnload.ToArray()))); AllowSceneActivation = arg.AllowSceneActivation; foreach (var scenePath in arg.ScenePathsToLoad) { AsyncOperation operation = SceneManager.LoadSceneAsync(scenePath, LoadSceneMode.Additive); operation.allowSceneActivation = false; asyncOperations.Add(operation); while (!operation.isDone) { float currentProgress = operation.progress; Updated.Invoke(currentProgress / arg.NumberOfScenesToLoad + Progress); if (currentProgress == 0.9f) { break; } yield return(null); } Progress += (0.9f / arg.NumberOfScenesToLoad); } while (!asyncOperations.TrueForAll(operation => operation.isDone)) { if (AllowSceneActivation) { for (int i = 0; i < asyncOperations.Count; i++) { asyncOperations[i].allowSceneActivation = true; while (!asyncOperations[i].isDone) { yield return(null); } if (arg.HasSceneToSetActive) { if (i == arg.ActiveSceneIndex) { SetActiveScene(arg.ScenePathsToLoad[i]); } } else if (i == 0) { SetActiveScene(arg.ScenePathsToLoad[0]); } Progress += (0.1f / arg.NumberOfScenesToLoad); Updated.Invoke(Progress); } } yield return(null); } Completed.Invoke(); yield return(SceneManager.UnloadSceneAsync(tempScene)); if (arg.HasTransitionScene && arg.AutomaticallyUnloadTransitionScene) { yield return(StartCoroutine(UnloadSceneAsyncCoroutine(false, arg.TransitionScenePath))); } }
private IEnumerator LoadAdditiveSceneAsyncCoroutine(SceneLoadArg arg) { if (arg.HasTransitionScene) { yield return(StartCoroutine(LoadAdditiveCoroutine(arg.TransitionScenePath))); } AllowSceneActivation = arg.AllowSceneActivation; foreach (var scenePath in arg.ScenePathsToLoad) { AsyncOperation operation = SceneManager.LoadSceneAsync(scenePath, LoadSceneMode.Additive); operation.allowSceneActivation = false; asyncOperations.Add(operation); while (!operation.isDone) { float currentProgress = operation.progress; Updated.Invoke(currentProgress / arg.NumberOfScenesToLoad + Progress); if (currentProgress == 0.9f) { break; } yield return(null); } Progress += (0.9f / arg.NumberOfScenesToLoad); } while (!asyncOperations.TrueForAll(operation => operation.isDone)) { if (AllowSceneActivation) { for (int i = 0; i < asyncOperations.Count; i++) { asyncOperations[i].allowSceneActivation = true; while (!asyncOperations[i].isDone) { yield return(null); } if (arg.HasSceneToSetActive) { if (i == arg.ActiveSceneIndex) { SetActiveScene(arg.ScenePathsToLoad[i]); } } Progress += (0.1f / arg.NumberOfScenesToLoad); Updated.Invoke(Progress); } } yield return(null); } Completed.Invoke(); if (arg.HasTransitionScene && arg.AutomaticallyUnloadTransitionScene) { yield return(StartCoroutine(UnloadSceneAsyncCoroutine(false, arg.TransitionScenePath))); } }