public void LoadScene( string sceneName, bool usesFade = false, UnloadCondition unloadCondition = UnloadCondition.AfterNewSceneHasLoaded) { if (IsLoadingScene) { return; } if (!IsSceneInBuild(sceneName, out int index)) { Debug.LogError($"Could not find scene {sceneName}. Make sure it is included inside Build Settings."); return; } StartCoroutine(Internal_LoadScene(sceneName, usesFade, unloadCondition)); }
private IEnumerator Internal_LoadScene( string sceneName, bool usesFade = true, UnloadCondition unloadCondition = UnloadCondition.AfterNewSceneHasLoaded ) { float initialTime = Time.realtimeSinceStartup; Scene activeScene = SceneManager.GetActiveScene(); IsLoadingScene = true; IsSceneInBuild(sceneName, out int newSceneIndex); OnSceneIsGoingToLoad?.Invoke(activeScene.buildIndex, newSceneIndex); SceneTransition sceneTransition = FindObjectOfType <SceneTransition>(); bool fadeConditionsMet = usesFade && sceneTransition != null; if (unloadCondition == UnloadCondition.BeforeNewSceneLoads) { yield return(SceneManager.UnloadSceneAsync(activeScene)); } var asyncScene = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); asyncScene.allowSceneActivation = false; if (fadeConditionsMet) { sceneTransition.FadeIn(); yield return(WaitForSecondsUnscaled(sceneTransition.TransitionDuration)); OnTransitionFadedIn?.Invoke(); } //if (fadeConditionsMet && unloadCondition == UnloadCondition.AfterTransitionFadedIn) { //yield return SceneManager.UnloadSceneAsync(activeScene); //} while (!asyncScene.isDone) { LoadingProgress = asyncScene.progress; OnSceneProgressUpdated?.Invoke(LoadingProgress); if (asyncScene.progress >= 0.9f && Time.realtimeSinceStartup - initialTime >= _minLoadTime ) { asyncScene.allowSceneActivation = true; } yield return(null); } OnSceneProgressUpdated?.Invoke(1f); if (fadeConditionsMet) { OnTransitionIsGoingToFadeOut?.Invoke(); yield return(WaitForSecondsUnscaled(sceneTransition.BeforeFadeStallDuration)); sceneTransition.FadeOut(); yield return(WaitForSecondsUnscaled(sceneTransition.TransitionDuration)); } IsLoadingScene = false; OnSceneLoaded?.Invoke(activeScene.buildIndex, newSceneIndex); if (unloadCondition == UnloadCondition.AfterNewSceneHasLoaded) { SceneManager.UnloadSceneAsync(activeScene); } }