/// <summary> /// Loads the scene. /// </summary> /// <returns>The async.</returns> /// <param name="sceneID">Scene I.</param> /// <param name="additive">If set to <c>true</c> additive.</param> /// <param name="makeActive">If set to <c>true</c> make active.</param> public AsyncOperation LoadAsync(string sceneID, bool additive = true, bool makeActive = true) { AsyncOperation _asyncOp = null; if (!IsSceneLoaded(sceneID)) { _asyncOp = SceneManager.LoadSceneAsync(sceneID, additive ? LoadSceneMode.Additive : LoadSceneMode.Single); if (_asyncOp != null) { _asyncOp.AsAsyncOperationObservable() .Last() .Subscribe(e => { Debug.Log(sceneID + " loaded"); //Fire signal trigger _eventService.Publish(new Events.SceneLoadedEvent { sceneID = sceneID }); //Set scene to active if (!additive || makeActive) { SetActiveScene(sceneID); } }); } } else { _asyncOp = new AsyncOperation(); //Fire signal trigger _eventService.Publish(new Events.SceneLoadedEvent { sceneID = sceneID });; if (makeActive) { //activate scene if it is currently disabled ActivateScene(sceneID); } //Set scene to active if (!additive || makeActive) { SetActiveScene(sceneID); } } return(_asyncOp); }
/// <summary> /// シーンを遷移する /// /// 時系列: -> t /// |次シーンロード|前シーンアンロード|フェードイン| /// |フェードアウト| /// </summary> /// <param name="sceneName">移動先シーン名</param> public void ChangeScene(string sceneName) { if (IsSceneMoving) { return; // 既にシーン移動中なら遷移処理をキャンセル } IsSceneMoving = true; // シーン移動中フラグを立てる var currentScene = SceneManager.GetActiveScene(); // 次シーンロード開始 AsyncOperation loading = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); loading.allowSceneActivation = false; // フェードアウト開始 bool isFadeOutDone = false; StartFade(isFadeOut: true, onFinished: () => { loading.allowSceneActivation = true; isFadeOutDone = true; }); // 次シーンロード終了&フェードアウト完了したら前シーンアンロード loading.AsAsyncOperationObservable() .Where(obj => obj.isDone && isFadeOutDone) .First() .Subscribe(_ => { // 前シーンアンロード開始 var unloading = SceneManager.UnloadSceneAsync(currentScene); // アンロード完了したらフェードイン開始 unloading.AsAsyncOperationObservable() .Where(val => val.isDone) .First() .Subscribe(__ => { StartFade(isFadeOut: false, onFinished: () => { // フェード完了時シーン移動中フラグを折る IsSceneMoving = false; }); }); }); }
/// <summary> /// Unload the specified scene. /// </summary> /// <param name="sceneName">Scene name.</param> public void Unload(string sceneID) { //Do nothing if scene is not loaded if (!SceneManager.GetSceneByName(sceneID).isLoaded) { return; } AsyncOperation _asyncUnload = SceneManager.UnloadSceneAsync(sceneID); //Do nothing if there is no scene if (_asyncUnload == null) { Debug.Log(sceneID + " not in active hirachy. Could not unload"); return; } _asyncUnload.AsAsyncOperationObservable().Subscribe( e => { //Do stuff while unloading }, error => { Debug.Log("Error unloading scene: " + sceneID); _eventService.Publish(new Events.SceneUnloadedEvent() { hasError = true, sceneID = sceneID }); }, () => { //Unload finished _eventService.Publish(new Events.SceneUnloadedEvent() { hasError = false, sceneID = sceneID }); Debug.Log(sceneID + " unloaded"); } ); }