Skip to content

Commit b356ce7

Browse files
author
Rene Damm
authored
FIX: Broken UI input when switching between assets (case 1371332, #1462).
1 parent eb4f139 commit b356ce7

4 files changed

Lines changed: 67 additions & 23 deletions

File tree

Assets/Tests/InputSystem/CoreTests_Editor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,6 +3116,7 @@ public StubInputControlPickerDropdown(InputControlPickerState state, Action<stri
31163116
{
31173117
}
31183118

3119+
#pragma warning disable CS0114
31193120
public UnityEngine.InputSystem.Editor.AdvancedDropdownItem BuildRoot()
31203121
{
31213122
return base.BuildRoot();

Assets/Tests/InputSystem/Plugins/UITests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,59 @@ public void UI_WhenDisablingInputModule_ActionsAreNotDisabledIfTheyWereNotEnable
28872887
Assert.That(pointAction.enabled, Is.True);
28882888
}
28892889

2890+
// https://fogbugz.unity3d.com/f/cases/1371332/
2891+
[UnityTest]
2892+
[Category("UI")]
2893+
public IEnumerator UI_WhenAssigningInputModuleActionAsset_OldInputsAreDisconnected_AndNewInputsAreConnected()
2894+
{
2895+
var mouse1 = InputSystem.AddDevice<Mouse>();
2896+
var mouse2 = InputSystem.AddDevice<Mouse>();
2897+
2898+
var eventSystemGO = new GameObject();
2899+
eventSystemGO.AddComponent<EventSystem>();
2900+
var inputModule = eventSystemGO.AddComponent<InputSystemUIInputModule>();
2901+
2902+
yield return null;
2903+
2904+
var actions1 = new DefaultInputActions();
2905+
actions1.devices = new[] { mouse1 };
2906+
2907+
inputModule.actionsAsset = actions1.asset;
2908+
2909+
Assert.That(actions1.UI.enabled, Is.True);
2910+
2911+
Set(mouse1.position, new Vector2(123, 234));
2912+
Set(mouse2.position, new Vector2(234, 345));
2913+
2914+
yield return null;
2915+
2916+
Assert.That(inputModule.m_CurrentPointerType, Is.EqualTo(UIPointerType.MouseOrPen));
2917+
Assert.That(inputModule.m_PointerStates[0].screenPosition, Is.EqualTo(new Vector2(123, 234)));
2918+
Assert.That(inputModule.m_PointerStates[0].eventData.device, Is.SameAs(mouse1));
2919+
2920+
var actions2 = new DefaultInputActions();
2921+
actions2.devices = new[] { mouse2 };
2922+
2923+
actions1.Disable();
2924+
2925+
inputModule.actionsAsset = actions2.asset;
2926+
2927+
Assert.That(actions1.UI.enabled, Is.False);
2928+
Assert.That(actions2.UI.enabled, Is.False);
2929+
2930+
actions1.Enable();
2931+
actions2.Enable();
2932+
2933+
Set(mouse1.position, new Vector2(234, 345));
2934+
Set(mouse2.position, new Vector2(345, 456));
2935+
2936+
yield return null;
2937+
2938+
Assert.That(inputModule.m_CurrentPointerType, Is.EqualTo(UIPointerType.MouseOrPen));
2939+
Assert.That(inputModule.m_PointerStates[0].screenPosition, Is.EqualTo(new Vector2(345, 456)));
2940+
Assert.That(inputModule.m_PointerStates[0].eventData.device, Is.SameAs(mouse2));
2941+
}
2942+
28902943
[UnityTest]
28912944
[Category("UI")]
28922945
[PrebuildSetup(typeof(InputSystemUIInputModuleTestScene_Setup))]

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ however, it has to be formatted properly to pass verification tests.
2424
- Fixed writing values into the half-axis controls of sticks (such as `Gamepad.leftStick.left`) producing incorrect values on the stick ([case 1336240](https://issuetracker.unity3d.com/issues/inputtestfixture-tests-return-inverted-values-when-pressing-gamepads-left-or-down-joystick-buttons)).
2525
- Fixed setting size of event trace in input debugger always growing back to largest size set before.
2626
- Fixed successive clicks not getting triggered with `TouchSimulation` on when not moving the mouse in-between clicks ([case 1330014](https://issuetracker.unity3d.com/issues/onclick-isnt-triggered-on-the-second-click-when-the-mouse-isnt-moved-and-simulate-touch-input-from-mouse-or-pen-is-enabled)).
27+
- Fixed `InputSystemUIInputModule` stopping to listen for input when swapping `InputActionAsset` instances while input was disabled ([case 1371332](https://issuetracker.unity3d.com/issues/ui-navigation-stops-working-after-adding-two-input-devices-to-a-scene)).
2728
- Fixed `InputSystemUIInputModule` showing incorrect bindings after pressing the 'Fix UI Input Module' button in PlayerInput component([case 1319968](https://issuetracker.unity3d.com/product/unity/issues/guid/1319968/)).
2829
- Fixed an issue where UI button clicks could be ignored by `InputSystemUIInputModule` if modifying on-screen devices from Update() callbacks ([case 1365070](https://issuetracker.unity3d.com/product/unity/issues/guid/1365070)).
2930
- Fixed an issue with `InputSystemUIInputModule` that would cause UI to stop responding during play mode after changing a script file while Recompile and Continue mode is active, or by forcing a script recompile using `RequestScriptCompilation`([case 1324215](https://issuetracker.unity3d.com/product/unity/issues/guid/1324215/)).

Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,20 +1415,6 @@ private bool HasNoActions()
14151415
&& m_TrackedDevicePositionAction?.action == null;
14161416
}
14171417

1418-
private bool IsAnyActionEnabled()
1419-
{
1420-
return (m_PointAction?.action?.enabled ?? true) &&
1421-
(m_LeftClickAction?.action?.enabled ?? true) &&
1422-
(m_RightClickAction?.action?.enabled ?? true) &&
1423-
(m_MiddleClickAction?.action?.enabled ?? true) &&
1424-
(m_MoveAction?.action?.enabled ?? true) &&
1425-
(m_SubmitAction?.action?.enabled ?? true) &&
1426-
(m_CancelAction?.action?.enabled ?? true) &&
1427-
(m_ScrollWheelAction?.action?.enabled ?? true) &&
1428-
(m_TrackedDeviceOrientationAction?.action?.enabled ?? true) &&
1429-
(m_TrackedDevicePositionAction?.action?.enabled ?? true);
1430-
}
1431-
14321418
private void EnableAllActions()
14331419
{
14341420
EnableInputAction(m_PointAction);
@@ -2144,6 +2130,10 @@ private InputActionReference UpdateReferenceForNewAsset(InputActionReference act
21442130
if (oldAction == null)
21452131
return null;
21462132

2133+
var oldActionEnabled = oldAction.enabled;
2134+
if (oldActionEnabled)
2135+
DisableInputAction(actionReference);
2136+
21472137
var oldActionMap = oldAction.actionMap;
21482138
Debug.Assert(oldActionMap != null, "Not expected to end up with a singleton action here");
21492139

@@ -2155,7 +2145,11 @@ private InputActionReference UpdateReferenceForNewAsset(InputActionReference act
21552145
if (newAction == null)
21562146
return null;
21572147

2158-
return InputActionReference.Create(newAction);
2148+
var reference = InputActionReference.Create(newAction);
2149+
if (oldActionEnabled)
2150+
EnableInputAction(reference);
2151+
2152+
return reference;
21592153
}
21602154

21612155
public InputActionAsset actionsAsset
@@ -2165,9 +2159,6 @@ public InputActionAsset actionsAsset
21652159
{
21662160
if (value != m_ActionsAsset)
21672161
{
2168-
var wasEnabled = IsAnyActionEnabled();
2169-
2170-
DisableAllActions();
21712162
UnhookActions();
21722163

21732164
m_ActionsAsset = value;
@@ -2180,12 +2171,10 @@ public InputActionAsset actionsAsset
21802171
scrollWheel = UpdateReferenceForNewAsset(scrollWheel);
21812172
submit = UpdateReferenceForNewAsset(submit);
21822173
cancel = UpdateReferenceForNewAsset(cancel);
2174+
trackedDeviceOrientation = UpdateReferenceForNewAsset(trackedDeviceOrientation);
2175+
trackedDevicePosition = UpdateReferenceForNewAsset(trackedDevicePosition);
21832176

2184-
if (wasEnabled)
2185-
{
2186-
HookActions();
2187-
EnableAllActions();
2188-
}
2177+
HookActions();
21892178
}
21902179
}
21912180
}

0 commit comments

Comments
 (0)