Unity-Technologies / InputSystem

An efficient and versatile input system for Unity.
Other
1.43k stars 310 forks source link

FIX: InvalidOperationException thrown entering PlayMode if PWA changes didn't save (ISX-1953) #1907

Closed timkeo closed 6 months ago

timkeo commented 6 months ago

Description

Changes to Project-wide Actions within the Project Settings are saved (to the Asset) automatically when the window looses focus, however I found a few scenarios were this doesn't occur and changes to the ActionMap aren't properly saved causing an exception when entering PlayMode.

There's a strange disconnect between the problem this PR fixes and the issue detailed in the ticket. As I understand it, restoring the UI ActionMap updates the Actions state in memory but it links the "old" Asset within ReadFromJson.ToAsset() (from InputActionAsset.cs) to the ActionMap. Typically, the changes are saved and serialized to the Asset at which point the ToAsset() operation is performed again and the correct Asset is linked to the ActionMap, but if we skip saving the Asset (as is the case with this bug) the InputActionMap.m_Asset never gets updated and remains null.

Ensuring the Asset is always properly saved in ProjectSettings fixes this issue and may address other issues as well.

Changes made

Adds calls to SaveAssetOnFocusLost() within InputActionsEditorSettingsProvider to cover additional scenario in which the asset needs to be saved (FocusLost isn't fired in these cases):

Notes

I updated the ticket with repro steps to cover the previously mentioned scenarios, and so far haven't been able to discover any other corner cases.

Checklist

Before review:

During merge: