internal bool SwitchToStage(Stage stage, bool setAsFirstItemAfterMainStage, Analytics.ChangeType changeTypeAnalytics) { if (stage == null) { Debug.LogError("Cannot switch to new stage. Input stage is null"); return(false); } bool setPreviousSelection = stage.opened; StopAnimationPlaybackAndPreviewing(); Stage previousStage = currentStage; if (!previousStage.AskUserToSaveModifiedStageBeforeSwitchingStage()) { // User canceled switching stage. // If the history contains the new stage we do not destroy it (the user // can have clicked a previous stage in the breadcrumb bar). if (!stageHistory.Contains(stage)) { DestroyImmediate(stage); } return(false); } // User accepted to switch stage (and lose any data if not saved) // Here we save current Hierarchy and SceneView stage state beforeSwitchingAwayFromStage?.Invoke(previousStage); // Used by the Avatar Editor to exit its editing mode before opening new stage stageChanging?.Invoke(previousStage, stage); // Set/Add stage in m_NavigationHistory (and detect what stages should be removed) if (m_DebugLogging) { Debug.Log("Set Navigation History (setAsFirstItemAfterMainStage " + setAsFirstItemAfterMainStage); } var deleteStages = new List <Stage>(); if (setAsFirstItemAfterMainStage) { deleteStages = m_NavigationHistory.ClearHistory(); m_NavigationHistory.AddStage(stage); } else { if (m_NavigationHistory.TrySetToIndexOfItem(stage)) { deleteStages = m_NavigationHistory.ClearForwardHistoryAfterCurrentStage(); } else { deleteStages = m_NavigationHistory.ClearForwardHistoryAndAddItem(stage); } } m_Analytics.ChangingStageStarted(previousStage); if (m_DebugLogging) { Debug.Log("Activate new stage"); } // Activate stage after setting up the history above so objects loaded during ActivateStage can query current stage bool success; try { if (!stage.opened) { stage.opened = true; success = stage.OnOpenStage(); } else { stage.OnReturnToStage(); success = stage.isValid; } if (success) { if (m_DebugLogging) { Debug.Log("Deactivate previous stage"); } // Here the Hierarchy and SceneView sync's up to the new stage stage.setSelectionAndScrollWhenBecomingCurrentStage = setPreviousSelection; stageChanged?.Invoke(previousStage, stage); } } catch (Exception e) { success = false; Debug.LogError("Error while changing Stage: " + e); } if (success) { // Activation and changing stage succeeded. Now destroy removed stages if (m_DebugLogging) { Debug.Log("Destroying " + deleteStages.Count + " stages"); } // A previous existing stage can have been requested to set as the current so don't delete that deleteStages.Remove(stage); DeleteStagesInReverseOrder(deleteStages); // Here we update state that relies on old scenes having been destroyed entirely. afterSuccessfullySwitchedToStage?.Invoke(stage); m_Analytics.ChangingStageEnded(stage, changeTypeAnalytics); } else { if (m_DebugLogging) { Debug.Log("Switching stage failed (" + stage + ")"); } RecoverFromStageChangeError(previousStage, deleteStages); } return(success); }
public static void GoToStage(Stage stage, bool setAsFirstItemAfterMainStage) { StageNavigationManager.instance.SwitchToStage(stage, setAsFirstItemAfterMainStage, StageNavigationManager.Analytics.ChangeType.Unknown); }