/// <summary> /// Loads an additive scene. /// /// Additive scene can be either [Scenes.CONSTRUCT] or [Scenes.HUD]. /// </summary> /// <param name="beforeSceneUnload">Handler executed before scene is loaded. If null, default handler is executed.</param> /// <param name="onSceneUnload">Handler executed after scene is loaded. If null, default handler is executed.</param> IEnumerator LoadScene(Scenes scene, BeforeSceneLoadDelegate beforeSceneLoad, OnSceneLoadDelegate onSceneLoad) { if (currentScene != Scenes.NONE) { throw new Exception("An error happened. There is another scene loaded. This can't be."); } //String sceneName = SceneNameForScene(scene); int sceneName = SceneNameForScene(scene); //if (sceneName != "") { if (sceneName != -1) { if (beforeSceneLoad != null) { beforeSceneLoad(); } else { DefaultBeforeSceneLoad(); } AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); if (asyncOperation != null) { asyncOperation.completed += delegate(AsyncOperation op) { currentScene = scene; if (onSceneLoad != null) { onSceneLoad(); } else { DefaultOnSceneLoad(); } }; } } yield break; }
/// <summary> /// Performs a scene change. First Unloads a scene, then Load the next Scene /// </summary> /// <param name="newScene">the new scene that shall be loaded</param> /// <param name="beforeSceneUnload">delegate that is executed before the current scene is unloaded</param> /// <param name="onSceneUnload">delegate that ist executed after the current scene is unloaded</param> /// <param name="beforeSceneLoad">delegate that is executed before the new scene is loaded</param> /// <param name="onSceneLoad">delegate that is executed after the new scene is loaded</param> public void ChangeScene(Scenes newScene, BeforeSceneUnloadDelegate beforeSceneUnload, OnSceneUnloadDelegate onSceneUnload, BeforeSceneLoadDelegate beforeSceneLoad, OnSceneLoadDelegate onSceneLoad) { StartCoroutine(UnloadScene(newScene, beforeSceneUnload, onSceneUnload, beforeSceneLoad, onSceneLoad)); }
/// <summary> /// Unloads an additive scene. /// /// Additive scene can be either [Scenes.CONSTRUCT] or [Scenes.HUD]. /// </summary> /// <param name="beforeSceneUnload">Handler executed before scene is unloaded. If null, default handler is executed.</param> /// <param name="onSceneUnload">Handler executed after scene is unloaded. If null, default handler is executed.</param> IEnumerator UnloadScene(Scenes newScene, BeforeSceneUnloadDelegate beforeSceneUnload, OnSceneUnloadDelegate onSceneUnload, BeforeSceneLoadDelegate beforeSceneLoad, OnSceneLoadDelegate onSceneLoad) { if (currentScene == Scenes.NONE) { // Load new scene, as no scene is currently loaded StartCoroutine(LoadScene(newScene, beforeSceneLoad, onSceneLoad)); yield break; } String sceneName = SceneNameForScene(currentScene); if (sceneName != "") { if (beforeSceneUnload != null) { beforeSceneUnload(); } else { DefaultBeforeSceneUnload(); } AsyncOperation asyncOperation = SceneManager.UnloadSceneAsync(sceneName, UnloadSceneOptions.UnloadAllEmbeddedSceneObjects); if (asyncOperation != null) { asyncOperation.completed += delegate(AsyncOperation op) { currentScene = Scenes.NONE; if (onSceneUnload != null) { onSceneUnload(); } else { DefaultOnSceneUnload(); } // Load new scene, after current scene has been unloaded StartCoroutine(LoadScene(newScene, beforeSceneLoad, onSceneLoad)); }; } } }