Skip to content

Commit afb1c33

Browse files
FIX: Editor closes when clicking cancel in a save dialog prompt (UUM-134748) (#2366)
Co-authored-by: Morgan Hoarau <122548697+MorganHoarau@users.noreply.github.com>
1 parent 498eb1a commit afb1c33

4 files changed

Lines changed: 37 additions & 17 deletions

File tree

Assets/Tests/InputSystem/APIVerificationTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,9 @@ public void API_MonoBehavioursHaveHelpUrls()
243243
var monoBehaviourTypes = typeof(InputSystem).Assembly.ExportedTypes.Where(t =>
244244
t.IsPublic && !t.IsAbstract && !IgnoreTypeForDocsByName(t.FullName) && !IgnoreTypeForDocsByNamespace(t.Namespace) &&
245245
typeof(MonoBehaviour).IsAssignableFrom(t));
246-
var monoBehaviourTypesHelpUrls =
247-
monoBehaviourTypes.Where(t => t.GetCustomAttribute<HelpURLAttribute>() != null)
248-
.Select(t => t.GetCustomAttribute<HelpURLAttribute>().URL);
249-
var monoBehaviourTypesWithoutHelpUrls =
250-
monoBehaviourTypes.Where(t => t.GetCustomAttribute<HelpURLAttribute>() == null);
251246

247+
var monoBehaviourTypesHelpUrls = monoBehaviourTypes.Where(t => t.GetCustomAttributes<HelpURLAttribute>().Any()).Select(t => t.GetCustomAttributes<HelpURLAttribute>().First().URL);
248+
var monoBehaviourTypesWithoutHelpUrls = monoBehaviourTypes.Where(t => !t.GetCustomAttributes<HelpURLAttribute>().Any());
252249
Assert.That(monoBehaviourTypesWithoutHelpUrls, Is.Empty);
253250
Assert.That(monoBehaviourTypesHelpUrls, Has.All.StartWith(InputSystem.kDocUrl));
254251
}

Assets/Tests/InputSystem/DocumentationBasedAPIVerficationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,16 @@ public void API_MonoBehaviourHelpUrlsAreValid()
127127
typeof(MonoBehaviour).IsAssignableFrom(t));
128128

129129
var monoBehaviourTypesWithHelpUrls = monoBehaviourTypes
130-
.Where(t => t.GetCustomAttribute<HelpURLAttribute>() != null);
130+
.Where(t => t.GetCustomAttributes<HelpURLAttribute>().Any());
131131

132132
var brokenHelpUrlErrors = new StringBuilder();
133133

134134
// Ensure the links are actually valid.
135135
foreach (var monoBehaviorTypeWithHelpUrl in monoBehaviourTypesWithHelpUrls)
136136
{
137137
// Get url
138-
var url = monoBehaviorTypeWithHelpUrl.GetCustomAttribute<HelpURLAttribute>().URL;
138+
var test = monoBehaviorTypeWithHelpUrl.GetCustomAttributes<HelpURLAttribute>();
139+
var url = test.FirstOrDefault()?.URL;
139140

140141
// Parse file path and anchor.
141142
var path = url.Substring(InputSystem.kDocUrl.Length);

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313
- Fixed auto-save not working for Input System actions in Project Settings when both the Project Settings and Input System Actions windows were open [UUM-134035](https://jira.unity3d.com/browse/UUM-134035)
1414
- Improved New Input System warning dialog, Native Device Inputs Not Enabled [UUM-132151].
1515
- Fixed caching for InputControlPath display name [ISX-2501](https://jira.unity3d.com/browse/ISX-2501)
16+
- 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)
1617

1718
### Changed
1819

Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/InputActionsEditorWindow.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static InputActionsEditorWindow()
2424

2525
private string m_AssetJson;
2626
private bool m_IsDirty;
27+
private bool m_IsEditorQuitting;
2728

2829
private StateContainer m_StateContainer;
2930
private InputActionsEditorView m_View;
@@ -333,11 +334,21 @@ private void DirtyInputActionsEditorWindow(InputActionsEditorState newState)
333334
private void OnEnable()
334335
{
335336
analytics.Begin();
337+
EditorApplication.wantsToQuit += OnWantsToQuit;
336338
}
337339

338340
private void OnDisable()
339341
{
340342
analytics.End();
343+
EditorApplication.wantsToQuit -= OnWantsToQuit;
344+
}
345+
346+
private bool OnWantsToQuit()
347+
{
348+
// Here the user will be prompted
349+
bool isAllowedToQuit = CheckCanCloseAndPromptIfDirty(false);
350+
m_IsEditorQuitting = isAllowedToQuit;
351+
return m_IsEditorQuitting;
341352
}
342353

343354
private void OnFocus()
@@ -362,39 +373,49 @@ private void OnLostFocus()
362373
analytics.RegisterEditorFocusOut();
363374
}
364375

365-
private void HandleOnDestroy()
376+
/// <summary>
377+
/// Shows a dialog when trying to close an input asset without saving changes.
378+
/// </summary>
379+
/// <param name="rebuildUIOnCancel">If true, reopens the editor window when user cancels.</param>
380+
/// <returns> Returns true if you should allow the Unity Editor to close. </returns>
381+
private bool CheckCanCloseAndPromptIfDirty(bool rebuildUIOnCancel)
366382
{
367383
// Do we have unsaved changes that we need to ask the user to save or discard?
368-
if (!m_IsDirty)
369-
return;
384+
// Early out if asset up to date or editor closing.
385+
if (!m_IsDirty || m_IsEditorQuitting)
386+
return true;
370387

371388
// Get target asset path from GUID, if this fails file no longer exists and we need to abort.
372389
var assetPath = AssetDatabase.GUIDToAssetPath(m_AssetGUID);
373390
if (string.IsNullOrEmpty(assetPath))
374-
return;
391+
return true;
375392

376393
// Prompt user with a dialog
377394
var result = Dialog.InputActionAsset.ShowSaveChanges(assetPath);
378395
switch (result)
379396
{
380397
case Dialog.Result.Save:
381398
Save(isAutoSave: false);
382-
break;
399+
return true;
383400
case Dialog.Result.Cancel:
384-
// Cancel editor quit. (open new editor window with the edited asset)
385-
ReshowEditorWindowWithUnsavedChanges();
386-
break;
401+
if (rebuildUIOnCancel)
402+
{
403+
// Cancel editor quit. (open new editor window with the edited asset)
404+
ReshowEditorWindowWithUnsavedChanges();
405+
}
406+
407+
return false;
387408
case Dialog.Result.Discard:
388409
// Don't save, quit - reload the old asset from the json to prevent the asset from being dirtied
389-
break;
410+
return true;
390411
default:
391412
throw new ArgumentOutOfRangeException(nameof(result));
392413
}
393414
}
394415

395416
private void OnDestroy()
396417
{
397-
HandleOnDestroy();
418+
CheckCanCloseAndPromptIfDirty(true);
398419

399420
// Clean-up
400421
CleanupStateContainer();

0 commit comments

Comments
 (0)