diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index 71a26a51d7..c15fc036df 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Improved New Input System warning dialog, Native Device Inputs Not Enabled [UUM-132151]. - Fixed caching for InputControlPath display name [ISX-2501](https://jira.unity3d.com/browse/ISX-2501) +- Fixed editor closing and not saving the input asset when clicking cancel in the dialog prompt [UUM-134748](https://jira.unity3d.com/browse/UUM-134748) ### Changed diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs index 9a6ec57263..1dfd324660 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs @@ -24,6 +24,7 @@ static InputActionsEditorWindow() private string m_AssetJson; private bool m_IsDirty; + private bool m_IsEditorQuitting; private StateContainer m_StateContainer; private InputActionsEditorView m_View; @@ -316,11 +317,21 @@ private void DirtyInputActionsEditorWindow(InputActionsEditorState newState) private void OnEnable() { analytics.Begin(); + EditorApplication.wantsToQuit += OnWantsToQuit; } private void OnDisable() { analytics.End(); + EditorApplication.wantsToQuit -= OnWantsToQuit; + } + + private bool OnWantsToQuit() + { + // Here the user will be prompted + bool isAllowedToQuit = HandleOnDestroyIfApplicationIsAllowedToQuit(false); + m_IsEditorQuitting = isAllowedToQuit; + return m_IsEditorQuitting; } private void OnFocus() @@ -345,39 +356,52 @@ private void OnLostFocus() analytics.RegisterEditorFocusOut(); } - private void HandleOnDestroy() + /// + /// Shows a dialog when trying to close an input asset without saving changes. + /// + /// Returns true if you should allow the Unity Editor to close. + private bool HandleOnDestroyIfApplicationIsAllowedToQuit(bool rebuildUIOnCancel) { // Do we have unsaved changes that we need to ask the user to save or discard? if (!m_IsDirty) - return; + return true; // Get target asset path from GUID, if this fails file no longer exists and we need to abort. var assetPath = AssetDatabase.GUIDToAssetPath(m_AssetGUID); if (string.IsNullOrEmpty(assetPath)) - return; + return true; - // Prompt user with a dialog - var result = Dialog.InputActionAsset.ShowSaveChanges(assetPath); - switch (result) + if (!m_IsEditorQuitting) { - case Dialog.Result.Save: - Save(isAutoSave: false); - break; - case Dialog.Result.Cancel: - // Cancel editor quit. (open new editor window with the edited asset) - ReshowEditorWindowWithUnsavedChanges(); - break; - case Dialog.Result.Discard: - // Don't save, quit - reload the old asset from the json to prevent the asset from being dirtied - break; - default: - throw new ArgumentOutOfRangeException(nameof(result)); + // Prompt user with a dialog + var result = Dialog.InputActionAsset.ShowSaveChanges(assetPath); + switch (result) + { + case Dialog.Result.Save: + Save(isAutoSave: false); + return true; + case Dialog.Result.Cancel: + if (rebuildUIOnCancel) + { + // Cancel editor quit. (open new editor window with the edited asset) + ReshowEditorWindowWithUnsavedChanges(); + } + + return false; + case Dialog.Result.Discard: + // Don't save, quit - reload the old asset from the json to prevent the asset from being dirtied + return true; + default: + throw new ArgumentOutOfRangeException(nameof(result)); + } } + + return true; } private void OnDestroy() { - HandleOnDestroy(); + HandleOnDestroyIfApplicationIsAllowedToQuit(true); // Clean-up CleanupStateContainer();