Skip to content

Commit 7a6cb91

Browse files
authored
FIX: Project wide actions enabling actions overrides PlayerInput (ISXB-920) (#2144)
1 parent c39efcb commit 7a6cb91

File tree

4 files changed

+65
-33
lines changed

4 files changed

+65
-33
lines changed

Assets/Tests/InputSystem/Plugins/PlayerInputTests.cs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void PlayerInput_CanInstantiatePlayer()
5555

5656
Assert.That(player, Is.Not.Null);
5757
Assert.That(player.playerIndex, Is.EqualTo(0));
58-
Assert.That(player.actions, Is.SameAs(prefabPlayerInput.actions));
58+
Assert.That(player.actions.actionMaps.Count, Is.EqualTo(prefabPlayerInput.actions.actionMaps.Count));
5959
Assert.That(player.devices, Is.EquivalentTo(new[] { gamepad }));
6060
Assert.That(player.currentControlScheme, Is.EqualTo("Gamepad"));
6161
}
@@ -108,7 +108,6 @@ public void PlayerInput_CanLinkSpecificDeviceToUI()
108108
var ui = prefab.AddComponent<InputSystemUIInputModule>();
109109
player.uiInputModule = ui;
110110
player.actions = InputActionAsset.FromJson(kActions);
111-
ui.actionsAsset = player.actions;
112111

113112
InputSystem.AddDevice<Gamepad>();
114113
InputSystem.AddDevice<Keyboard>();
@@ -117,6 +116,7 @@ public void PlayerInput_CanLinkSpecificDeviceToUI()
117116
var gamepad = InputSystem.AddDevice<Gamepad>();
118117

119118
var instance = PlayerInput.Instantiate(prefab, pairWithDevices: gamepad);
119+
ui.actionsAsset = instance.actions;
120120

121121
Assert.That(instance.devices, Is.EquivalentTo(new[] { gamepad }));
122122
Assert.That(ui.actionsAsset.devices, Is.EquivalentTo(new[] { gamepad }));
@@ -149,11 +149,11 @@ public void PlayerInput_CanUseSameActionsForUIInputModule()
149149
eventSystemGO.SetActive(true);
150150
playerGO.SetActive(true);
151151

152-
Assert.That(actions.FindActionMap("Gameplay").enabled, Is.True);
153-
Assert.That(actions.FindActionMap("UI").enabled, Is.True);
154-
Assert.That(actions["UI/Navigate"].controls, Is.Empty);
155-
Assert.That(actions["UI/Point"].controls, Is.EquivalentTo(new[] { mouse.position }));
156-
Assert.That(actions["UI/Click"].controls, Is.EquivalentTo(new[] { mouse.leftButton }));
152+
Assert.That(player.actions.FindActionMap("Gameplay").enabled, Is.True);
153+
Assert.That(uiModule.actionsAsset.FindActionMap("UI").enabled, Is.True);
154+
Assert.That(uiModule.actionsAsset["UI/Navigate"].controls, Is.Empty);
155+
Assert.That(uiModule.actionsAsset["UI/Point"].controls, Is.EquivalentTo(new[] { mouse.position }));
156+
Assert.That(uiModule.actionsAsset["UI/Click"].controls, Is.EquivalentTo(new[] { mouse.leftButton }));
157157
}
158158

159159
[Test]
@@ -391,7 +391,23 @@ public void PlayerInput_CanAssignActionsToPlayer()
391391
var actions = InputActionAsset.FromJson(kActions);
392392
playerInput.actions = actions;
393393

394-
Assert.That(playerInput.actions, Is.SameAs(actions));
394+
Assert.That(playerInput.actions.actionMaps.Count, Is.EqualTo(actions.actionMaps.Count));
395+
Assert.That(playerInput.actions.actionMaps[0].name, Is.EqualTo(actions.actionMaps[0].name));
396+
}
397+
398+
[Test]
399+
[Category("PlayerInput")]
400+
public void PlayerInput_CopiesActionAssetForFirstPlayer()
401+
{
402+
var go = new GameObject();
403+
var playerInput = go.AddComponent<PlayerInput>();
404+
405+
var actions = InputActionAsset.FromJson(kActions);
406+
playerInput.actions = actions;
407+
408+
Assert.That(playerInput.actions.actionMaps.Count, Is.EqualTo(actions.actionMaps.Count));
409+
Assert.That(playerInput.actions.actionMaps[0].name, Is.EqualTo(actions.actionMaps[0].name));
410+
Assert.That(playerInput.actions.GetInstanceID(), !Is.EqualTo(actions.GetInstanceID()));
395411
}
396412

397413
[Test]
@@ -407,13 +423,13 @@ public void PlayerInput_AssigningNewActionsToPlayer_DisablesExistingActions()
407423
playerInput.defaultActionMap = "gameplay";
408424
playerInput.actions = actions1;
409425

410-
Assert.That(actions1.actionMaps[0].enabled, Is.True);
411-
Assert.That(actions2.actionMaps[0].enabled, Is.False);
426+
Assert.That(playerInput.actions.actionMaps[0].enabled, Is.True);
427+
Assert.That(actions1.actionMaps[0].enabled, Is.False);
412428

413429
playerInput.actions = actions2;
414430

415-
Assert.That(actions1.actionMaps[0].enabled, Is.False);
416-
Assert.That(actions2.actionMaps[0].enabled, Is.True);
431+
Assert.That(actions2.actionMaps[0].enabled, Is.False);
432+
Assert.That(playerInput.actions.actionMaps[0].enabled, Is.True);
417433
}
418434

419435
[Test]
@@ -1714,7 +1730,8 @@ InputDevice[] AddDevices()
17141730

17151731
// Make sure that no cloning of actions happened on the prefab.
17161732
// https://fogbugz.unity3d.com/f/cases/1319756/
1717-
Assert.That(playerPrefab.GetComponent<PlayerInput>().actions, Is.SameAs(playerPrefabActions));
1733+
1734+
Assert.That(playerPrefab.GetComponent<PlayerInput>().actions.actionMaps.Count, Is.EqualTo(playerPrefabActions.actionMaps.Count));
17181735
Assert.That(playerPrefab.GetComponent<PlayerInput>().m_ActionsInitialized, Is.False);
17191736
}
17201737

@@ -2421,13 +2438,13 @@ public void PlayerInput_DelegatesAreUpdate_WhenActionMapAddedAfterAssignment()
24212438
// Disable the asset while adding another action map to it as none
24222439
// of the actions in the asset can be enabled during modification
24232440
//
2424-
actionAsset.Disable();
2441+
playerInput.actions.Disable();
24252442
var keyboard = InputSystem.AddDevice<Keyboard>();
2426-
var newActionMap = actionAsset.AddActionMap("NewMap");
2443+
var newActionMap = playerInput.actions.AddActionMap("NewMap");
24272444
var newAction = newActionMap.AddAction("NewAction");
24282445
newAction.AddBinding("<Keyboard>/k", groups: "Keyboard");
2429-
actionAsset.AddControlScheme("Keyboard").WithRequiredDevice<Keyboard>();
2430-
actionAsset.Enable();
2446+
playerInput.actions.AddControlScheme("Keyboard").WithRequiredDevice<Keyboard>();
2447+
playerInput.actions.Enable();
24312448

24322449
playerInput.currentActionMap = newActionMap;
24332450
playerInput.ActivateInput();

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ however, it has to be formatted properly to pass verification tests.
1010

1111
## [Unreleased] - yyyy-mm-dd
1212

13+
### Fixed
14+
- Fixed an issue where all action maps were enabled initially for project wide actions, which overrode the PlayerInput action map configuration. [ISXB-920](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-920)
15+
1316
### Changed
1417
- Changed enum value `Key.IMESelected` to obsolete which was not a real key. Please use the ButtonControl `imeSelected`.
1518

Packages/com.unity.inputsystem/InputSystem/InputSystem.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3609,12 +3609,6 @@ internal static void InitializeInEditor(IInputRuntime runtime = null)
36093609
// this would cancel the import of large assets that are dependent on the InputSystem package and import it as a dependency.
36103610
EditorApplication.delayCall += ShowRestartWarning;
36113611

3612-
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
3613-
// Make sure project wide input actions are enabled.
3614-
// Note that this will always fail if entering play-mode within editor since not yet in play-mode.
3615-
EnableActions();
3616-
#endif
3617-
36183612
RunInitialUpdate();
36193613

36203614
k_InputInitializeInEditorMarker.End();
@@ -3657,9 +3651,9 @@ internal static void OnPlayModeChange(PlayModeStateChange change)
36573651
case PlayModeStateChange.EnteredPlayMode:
36583652
s_SystemObject.enterPlayModeTime = InputRuntime.s_Instance.currentTime;
36593653
s_Manager.SyncAllDevicesAfterEnteringPlayMode();
3660-
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
3654+
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
36613655
EnableActions();
3662-
#endif // UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
3656+
#endif // UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
36633657
break;
36643658

36653659
case PlayModeStateChange.ExitingPlayMode:

Packages/com.unity.inputsystem/InputSystem/Plugins/PlayerInput/PlayerInput.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,14 @@ public InputActionAsset actions
342342
UninitializeActions();
343343
}
344344

345+
var didChange = m_Actions != null;
346+
345347
m_Actions = value;
346348

349+
if (didChange || m_Enabled)
350+
// copy action asset for the first player so that the original asset stays untouched
351+
CopyActionAssetAndApplyBindingOverrides();
352+
347353
if (m_Enabled)
348354
{
349355
ClearCaches();
@@ -1373,14 +1379,7 @@ private void InitializeActions()
13731379
for (var i = 0; i < s_AllActivePlayersCount; ++i)
13741380
if (s_AllActivePlayers[i].m_Actions == m_Actions && s_AllActivePlayers[i] != this)
13751381
{
1376-
var oldActions = m_Actions;
1377-
m_Actions = Instantiate(m_Actions);
1378-
for (var actionMap = 0; actionMap < oldActions.actionMaps.Count; actionMap++)
1379-
{
1380-
for (var binding = 0; binding < oldActions.actionMaps[actionMap].bindings.Count; binding++)
1381-
m_Actions.actionMaps[actionMap].ApplyBindingOverride(binding, oldActions.actionMaps[actionMap].bindings[binding]);
1382-
}
1383-
1382+
CopyActionAssetAndApplyBindingOverrides();
13841383
break;
13851384
}
13861385

@@ -1430,6 +1429,18 @@ private void InitializeActions()
14301429
m_ActionsInitialized = true;
14311430
}
14321431

1432+
private void CopyActionAssetAndApplyBindingOverrides()
1433+
{
1434+
// duplicate action asset to not operate on the original (as it might be used outside - eg project wide action asset or UIInputModule)
1435+
var oldActions = m_Actions;
1436+
m_Actions = Instantiate(m_Actions);
1437+
for (var actionMap = 0; actionMap < oldActions.actionMaps.Count; actionMap++)
1438+
{
1439+
for (var binding = 0; binding < oldActions.actionMaps[actionMap].bindings.Count; binding++)
1440+
m_Actions.actionMaps[actionMap].ApplyBindingOverride(binding, oldActions.actionMaps[actionMap].bindings[binding]);
1441+
}
1442+
}
1443+
14331444
private void UninitializeActions()
14341445
{
14351446
if (!m_ActionsInitialized)
@@ -1799,6 +1810,13 @@ void Reset()
17991810

18001811
#endif
18011812

1813+
private void Awake()
1814+
{
1815+
// If an action asset is assigned copy it to avoid modifying the original asset.
1816+
if (m_Actions != null)
1817+
CopyActionAssetAndApplyBindingOverrides();
1818+
}
1819+
18021820
private void OnEnable()
18031821
{
18041822
m_Enabled = true;

0 commit comments

Comments
 (0)