diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index 6495e2125f..f8abaceb26 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -10,6 +10,9 @@ however, it has to be formatted properly to pass verification tests. ## [Unreleased] - yyyy-mm-dd +### Fixed +- Fixed an issue where removing a newly created action in the Asset Editor would cause an exception. [UUM-95693](https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-95693) + ## [1.13.0] - 2025-02-05 ### Fixed diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Commands/Commands.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Commands/Commands.cs index 8f461cb332..999f14353b 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Commands/Commands.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Commands/Commands.cs @@ -453,7 +453,7 @@ public static Command DeleteAction(int actionMapIndex, string actionName) return (in InputActionsEditorState state) => { var actionMap = Selectors.GetActionMapAtIndex(state, actionMapIndex)?.wrappedProperty; - var action = Selectors.GetActionInMap(state, actionMapIndex, actionName).wrappedProperty; + var action = Selectors.GetActionInMap(state, actionMapIndex, actionName)?.wrappedProperty; var actionIndex = action.GetIndexOfArrayElement(); var actionID = InputActionSerializationHelpers.GetId(action); var isCut = state.IsActionCut(actionMapIndex, actionIndex); @@ -638,10 +638,17 @@ public static Command ChangeActionName(int actionMapIndex, string oldName, strin return (in InputActionsEditorState state) => { var actionMap = Selectors.GetActionMapAtIndex(state, actionMapIndex)?.wrappedProperty; - var action = Selectors.GetActionInMap(state, actionMapIndex, oldName).wrappedProperty; - InputActionSerializationHelpers.RenameAction(action, actionMap, newName); - state.serializedObject.ApplyModifiedProperties(); - state.m_Analytics?.RegisterActionEdit(); + var action = Selectors.GetActionInMap(state, actionMapIndex, oldName)?.wrappedProperty; + // In an Asset Editor workflow, the serialized property might not exist in case a new action is created + // that is yet to be named, and it's deleted before it's named. + // This is a particular case when ActionTreeView::DeleteItem triggers + // Focus callback -> OnEditTextFinished -> ChangeActionName. + if (action != null) + { + InputActionSerializationHelpers.RenameAction(action, actionMap, newName); + state.serializedObject.ApplyModifiedProperties(); + state.m_Analytics?.RegisterActionEdit(); + } return state; }; } diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/Selectors.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/Selectors.cs index 831f4f93e5..f2a783f3b8 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/Selectors.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/UITKAssetEditor/Views/Selectors.cs @@ -152,12 +152,17 @@ public static SerializedProperty GetActionForIndex(SerializedProperty actionMap, return actionMap.FindPropertyRelative(nameof(InputActionMap.m_Actions)).GetArrayElementAtIndex(actionIndex); } - public static SerializedInputAction GetActionInMap(InputActionsEditorState state, int mapIndex, string name) + public static SerializedInputAction? GetActionInMap(InputActionsEditorState state, int mapIndex, string name) { - return new SerializedInputAction(state.serializedObject + SerializedProperty property = state.serializedObject ?.FindProperty(nameof(InputActionAsset.m_ActionMaps))?.GetArrayElementAtIndex(mapIndex) ?.FindPropertyRelative(nameof(InputActionMap.m_Actions)) - ?.FirstOrDefault(p => p.FindPropertyRelative(nameof(InputAction.m_Name)).stringValue == name)); + ?.FirstOrDefault(p => p.FindPropertyRelative(nameof(InputAction.m_Name)).stringValue == name); + + // If the action is not found, return null. + if (property == null) + return null; + return new SerializedInputAction(property); } public static SerializedInputBinding GetCompositeOrBindingInMap(SerializedProperty actionMap, int bindingIndex)