Skip to content

Commit 02e8b03

Browse files
authored
FIX: save and reset input action asset (ISX-1525, ISX-1483, ISX-1559) (#1727)
* work on copy of input actions asset (reset not working fix) * dirty input actions editor window if changes were made * show dialog on close input actions editor (WIP) * fixed cancel closing input asset editor * avoid changing asset name to (Clone) in the name * fixed saving logic for multiple open assets * only change header if needed * PR refactor - reload asset from json instead of cloning it * fixed reopening window after Cancel
1 parent b19428e commit 02e8b03

1 file changed

Lines changed: 77 additions & 6 deletions

File tree

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

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ internal class InputActionsEditorWindow : EditorWindow
2525
{
2626
private static readonly string k_FileExtension = "." + InputActionAsset.Extension;
2727
private int m_AssetId;
28+
private string m_AssetPath;
29+
private string m_AssetJson;
30+
private bool m_IsDirty;
2831

2932
[OnOpenAsset]
3033
public static bool OpenAsset(int instanceId, int line)
@@ -51,6 +54,7 @@ public static bool OpenAsset(int instanceId, int line)
5154
window.Focus();
5255
return true;
5356
}
57+
window.m_IsDirty = false;
5458
window.m_AssetId = instanceId;
5559
window.titleContent = new GUIContent("Input Actions Editor");
5660
window.SetAsset(asset);
@@ -74,8 +78,10 @@ private static InputActionsEditorWindow GetOrCreateWindow(int id, out bool isAlr
7478

7579
private void SetAsset(InputActionAsset asset)
7680
{
81+
m_AssetPath = AssetDatabase.GetAssetPath(asset);
7782
var serializedAsset = new SerializedObject(asset);
7883
m_State = new InputActionsEditorState(serializedAsset);
84+
m_AssetJson = File.ReadAllText(m_AssetPath);
7985
bool isGUIDObtained = AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out m_AssetGUID, out long _);
8086
Debug.Assert(isGUIDObtained, $"Failed to get asset {asset.name} GUID");
8187

@@ -95,6 +101,8 @@ private void CreateGUI()
95101
if (m_State.serializedObject == null)
96102
{
97103
var asset = GetAssetFromDatabase();
104+
m_AssetPath = AssetDatabase.GetAssetPath(asset);
105+
m_AssetJson = File.ReadAllText(m_AssetPath);
98106
var serializedAsset = new SerializedObject(asset);
99107
m_State = new InputActionsEditorState(m_State, serializedAsset);
100108
}
@@ -119,10 +127,70 @@ private void BuildUI()
119127

120128
private void OnStateChanged(InputActionsEditorState newState)
121129
{
130+
DirtyInputActionsEditorWindow(newState);
122131
if (InputEditorUserSettings.autoSaveInputActionAssets)
123132
SaveAsset(m_State.serializedObject);
124133
}
125134

135+
private void DirtyInputActionsEditorWindow(InputActionsEditorState newState)
136+
{
137+
var isWindowDirty = !InputEditorUserSettings.autoSaveInputActionAssets && HasAssetChanged(newState.serializedObject);
138+
if (m_IsDirty == isWindowDirty)
139+
return;
140+
m_IsDirty = isWindowDirty;
141+
titleContent = m_IsDirty ? new GUIContent("(*) Input Actions Editor") : new GUIContent("Input Actions Editor");
142+
}
143+
144+
private bool HasAssetChanged(SerializedObject serializedAsset)
145+
{
146+
var asset = (InputActionAsset)serializedAsset.targetObject;
147+
var newAssetJson = asset.ToJson();
148+
return newAssetJson != m_AssetJson;
149+
}
150+
151+
private void OnDestroy()
152+
{
153+
ConfirmSaveChangesIfNeeded();
154+
}
155+
156+
private void ConfirmSaveChangesIfNeeded()
157+
{
158+
// Do we have unsaved changes?
159+
if (!m_IsDirty)
160+
return;
161+
162+
var result = EditorUtility.DisplayDialogComplex("Input Action Asset has been modified", $"Do you want to save the changes you made in:\n{m_AssetPath}\n\nYour changes will be lost if you don't save them.", "Save", "Cancel", "Don't Save");
163+
switch (result)
164+
{
165+
case 0: // Save
166+
SaveAsset(m_State.serializedObject, this);
167+
break;
168+
case 1: // Cancel editor quit. (open new editor window with the edited asset)
169+
ReshowEditorWindowWithUnsavedChanges();
170+
break;
171+
case 2: // Don't save, quit - reload the old asset from the json to prevent the asset from being dirtied
172+
AssetDatabase.ImportAsset(m_AssetPath);
173+
break;
174+
}
175+
}
176+
177+
private void ReshowEditorWindowWithUnsavedChanges()
178+
{
179+
var window = CreateWindow<InputActionsEditorWindow>();
180+
CopyOldStatsToNewWindow(window);
181+
window.BuildUI();
182+
window.Show();
183+
}
184+
185+
private void CopyOldStatsToNewWindow(InputActionsEditorWindow window)
186+
{
187+
window.m_AssetId = m_AssetId;
188+
window.m_State = m_State;
189+
window.m_AssetPath = m_AssetPath;
190+
window.m_AssetJson = m_AssetJson;
191+
window.m_IsDirty = true;
192+
}
193+
126194
private InputActionAsset GetAssetFromDatabase()
127195
{
128196
Debug.Assert(!string.IsNullOrEmpty(m_AssetGUID), "Asset GUID is empty");
@@ -133,18 +201,21 @@ private InputActionAsset GetAssetFromDatabase()
133201
[SerializeField] private InputActionsEditorState m_State;
134202
[SerializeField] private string m_AssetGUID;
135203

136-
public static void SaveAsset(SerializedObject serializedAsset)
204+
public static void SaveAsset(SerializedObject serializedAsset, InputActionsEditorWindow currentWindow = null)
137205
{
206+
if ((focusedWindow == null || focusedWindow is not InputActionsEditorWindow) && currentWindow == null)
207+
return;
208+
currentWindow = currentWindow ? currentWindow : (InputActionsEditorWindow)focusedWindow;
138209
var asset = (InputActionAsset)serializedAsset.targetObject;
139-
var assetPath = AssetDatabase.GetAssetPath(asset);
140210
var assetJson = asset.ToJson();
141211

142-
var existingJson = File.ReadAllText(assetPath);
212+
var existingJson = File.ReadAllText(currentWindow.m_AssetPath);
143213
if (assetJson != existingJson)
144214
{
145-
EditorHelpers.CheckOut(assetPath);
146-
File.WriteAllText(assetPath, assetJson);
147-
AssetDatabase.ImportAsset(assetPath);
215+
EditorHelpers.CheckOut(currentWindow.m_AssetPath);
216+
File.WriteAllText(currentWindow.m_AssetPath, assetJson);
217+
AssetDatabase.ImportAsset(currentWindow.m_AssetPath);
218+
currentWindow.m_AssetJson = assetJson;
148219
}
149220
}
150221
}

0 commit comments

Comments
 (0)