Skip to content

Commit 652aed9

Browse files
FIX: PlayerInput does not work with C# wrappers (ISXB-1535) (#2188)
1 parent 05635ca commit 652aed9

File tree

9 files changed

+192
-77
lines changed

9 files changed

+192
-77
lines changed

.github/codecov.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ coverage:
99
round: down
1010
range: "50...70"
1111
status:
12-
patch: true
12+
patch:
13+
default:
14+
target: auto
15+
threshold: 1%
16+
base: auto
17+
if_ci_failed: success
18+
informational: true
19+
only_pulls: true
1320
default_rules:
1421
flag_coverage_not_uploaded_behavior: exclude
1522
project:
1623
default:
1724
target: auto
1825
threshold: 1%
19-
base: auto
26+
base: auto
2027
if_ci_failed: success
2128
informational: true
2229
only_pulls: true

Assets/Tests/InputSystem/CoreTests_Editor.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,9 +2893,9 @@ public void Editor_WhenRunUpdatesInEditModeIsEnabled_InputActionsTriggerInEditMo
28932893
private static void DisableProjectWideActions()
28942894
{
28952895
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
2896-
// If the system has project-wide input actions they will also trigger enable/disable via
2897-
// play mode change triggers above. Hence we adjust extra variable to compensate of
2898-
// state allocated by project-wide actions.
2896+
// If the system has project-wide input actions they will start enabled once InputSystem.Reset() is called and
2897+
// be disabled entering EditMode. Hence, we adjust extra variable to compensate ofstate allocated by
2898+
// project-wide actions.
28992899
if (InputSystem.actions)
29002900
{
29012901
Assert.That(InputActionState.s_GlobalState.globalList.length, Is.EqualTo(1));
@@ -2905,6 +2905,39 @@ private static void DisableProjectWideActions()
29052905
#endif
29062906
}
29072907

2908+
[Test]
2909+
[Category("Editor")]
2910+
public void Editor_InitializeInEditor_EnablesProjectWideActions()
2911+
{
2912+
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
2913+
if (InputSystem.actions != null)
2914+
{
2915+
// Asserts that project wide actions are enabled by default.
2916+
// Before the test is run, InputSystem.Reset() is called which will enable them.
2917+
// It can be interpreted as a mock of the behavior that happens when `InitializeInEditor()` is called.
2918+
Assert.That(InputSystem.actions.enabled, Is.True);
2919+
2920+
// Calling exit play mode callbacks will disable them
2921+
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingPlayMode);
2922+
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredEditMode);
2923+
2924+
Assert.That(InputSystem.actions.enabled, Is.False);
2925+
2926+
// Calling enter play mode callbacks will not re-enable them per default. They are only
2927+
// enabled when `InputSystem.InitializeInEditor()` is called, which happens before these callbacks.
2928+
// Note: Project-wide actions are disabled at this point. These next lines are added to make sure we
2929+
// establish behavior that project-wide actions should be enabled only once
2930+
// `InputSystem.InitializeInEditor()` is called. Before this test was introduced, project-wide actions were
2931+
// enabled after entering play mode again which would lead to a different behavior than Player
2932+
// builds.
2933+
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingEditMode);
2934+
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredPlayMode);
2935+
2936+
Assert.That(InputSystem.actions.enabled, Is.False);
2937+
}
2938+
#endif
2939+
}
2940+
29082941
[Test]
29092942
[Category("Editor")]
29102943
public void Editor_LeavingPlayMode_DestroysAllActionStates()
@@ -2918,6 +2951,15 @@ public void Editor_LeavingPlayMode_DestroysAllActionStates()
29182951

29192952
// Enter play mode.
29202953
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingEditMode);
2954+
2955+
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
2956+
2957+
// This simulates enabling project-wide actions, which is done before just before entering play mode,
2958+
// called from InputSystem.InitializeInEditor().
2959+
if (InputSystem.actions)
2960+
InputSystem.actions.Enable();
2961+
#endif
2962+
29212963
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredPlayMode);
29222964

29232965
DisableProjectWideActions();
@@ -2934,7 +2976,8 @@ public void Editor_LeavingPlayMode_DestroysAllActionStates()
29342976
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredEditMode);
29352977

29362978
Assert.That(InputActionState.s_GlobalState.globalList.length, Is.Zero);
2937-
Assert.That(InputSystem.s_Manager.m_StateChangeMonitors[0].listeners[0].control, Is.Null); // Won't get removed, just cleared.
2979+
// Won't get removed, just cleared.
2980+
Assert.That(InputSystem.s_Manager.m_StateChangeMonitors[0].listeners[0].control, Is.Null);
29382981
}
29392982

29402983
[Test]

Assets/Tests/InputSystem/CoreTests_ProjectWideActions.cs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
using NUnit.Framework;
55
using UnityEngine.InputSystem;
66
using UnityEngine.TestTools;
7+
using UnityEngine;
8+
using System.Collections;
79

810
#if UNITY_EDITOR
911
using System.IO;
1012
using UnityEditor;
11-
using UnityEngine;
1213
using UnityEngine.InputSystem.Editor;
1314
#endif // UNITY_EDITOR
1415

@@ -101,10 +102,10 @@ public void ProjectWideActions_IsAutomaticallyAssignedFromPersistedAsset_WhenRun
101102
Assert.That(InputSystem.actions, Is.Not.Null);
102103

103104
// In editor play-mode we may as well verify that the asset has the expected name
104-
#if UNITY_EDITOR
105+
#if UNITY_EDITOR
105106
var expectedName = InputActionImporter.NameFromAssetPath(AssetDatabase.GetAssetPath(InputSystem.actions));
106107
Assert.That(InputSystem.actions.name, Is.EqualTo(expectedName));
107-
#endif
108+
#endif
108109
}
109110

110111
[Test]
@@ -140,6 +141,61 @@ public void ProjectWideActions_AppearInEnabledActions()
140141

141142
// TODO Modifying the actions object after being assigned should also enable newly added actions?
142143
}
144+
145+
[Category(TestCategory)]
146+
[Test]
147+
[TestCase("Player", true, Description = "PlayerInput using project-wide actions can have default action map set " +
148+
"enabled, and all others disabled.")]
149+
[TestCase(null, false, Description = "PlayerInput using project wide actions has all action maps of project-wide " +
150+
"actions disabled, if there is no default action map assigned.")]
151+
public void ProjectWideActions_CanEnableCurrentActionMapOfPlayerInput(string actionMapName, bool expectedResult)
152+
{
153+
var keyboard = InputSystem.AddDevice<Keyboard>();
154+
var go = new GameObject("PlayerInput");
155+
156+
go.SetActive(false);
157+
var playerInput = go.AddComponent<PlayerInput>();
158+
playerInput.actions = InputSystem.actions;
159+
// Default action map to be enabled. All others are expected to be disabled since InputSystem.actions.Disable()
160+
// will be called.
161+
playerInput.defaultActionMap = actionMapName;
162+
163+
// PWA actions assigned to playerInput start enabled
164+
Assert.That(playerInput.actions.enabled, Is.True);
165+
Assert.That(ReferenceEquals(playerInput.actions, InputSystem.actions));
166+
167+
#if UNITY_EDITOR
168+
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingEditMode);
169+
#endif
170+
171+
// This makes sure to call PlayerInput.OnEnable()
172+
go.SetActive(true);
173+
174+
// This mimics usings to disabling actions on Start/Awake.
175+
InputSystem.actions.Disable();
176+
177+
if (!string.IsNullOrEmpty(actionMapName))
178+
playerInput.SwitchCurrentActionMap("Player");
179+
180+
Assert.That(playerInput.actions.enabled, Is.EqualTo(expectedResult));
181+
182+
// We do this on the editor to make sure project-wide actions maintain the enabled state
183+
// after entering PlayMode.
184+
#if UNITY_EDITOR
185+
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredPlayMode);
186+
Assert.That(playerInput.actions.enabled, Is.EqualTo(expectedResult));
187+
#endif
188+
189+
if (!string.IsNullOrEmpty(actionMapName))
190+
Assert.That(playerInput.currentActionMap.enabled, Is.EqualTo(expectedResult));
191+
192+
// Check state of all action maps in the asset
193+
Assert.That(InputSystem.actions.FindActionMap("Player").enabled, Is.EqualTo(expectedResult));
194+
Assert.That(InputSystem.actions.FindActionMap("UI").enabled, Is.False);
195+
196+
//NOTE: Asset actions are considered enabled even if a single action map is enabled
197+
Assert.That(playerInput.actions.enabled, Is.EqualTo(expectedResult));
198+
}
143199
}
144200

145201
#endif

Assets/Tests/InputSystem/Plugins/PlayerInputTests.cs

Lines changed: 19 additions & 18 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.actionMaps.Count, Is.EqualTo(prefabPlayerInput.actions.actionMaps.Count));
58+
Assert.That(player.actions, Is.SameAs(prefabPlayerInput.actions));
5959
Assert.That(player.devices, Is.EquivalentTo(new[] { gamepad }));
6060
Assert.That(player.currentControlScheme, Is.EqualTo("Gamepad"));
6161
}
@@ -395,21 +395,6 @@ public void PlayerInput_CanAssignActionsToPlayer()
395395
Assert.That(playerInput.actions.actionMaps[0].name, Is.EqualTo(actions.actionMaps[0].name));
396396
}
397397

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()));
411-
}
412-
413398
[Test]
414399
[Category("PlayerInput")]
415400
public void PlayerInput_AssigningNewActionsToPlayer_DisablesExistingActions()
@@ -424,12 +409,12 @@ public void PlayerInput_AssigningNewActionsToPlayer_DisablesExistingActions()
424409
playerInput.actions = actions1;
425410

426411
Assert.That(playerInput.actions.actionMaps[0].enabled, Is.True);
427-
Assert.That(actions1.actionMaps[0].enabled, Is.False);
412+
Assert.That(actions2.actionMaps[0].enabled, Is.False);
428413

429414
playerInput.actions = actions2;
430415

431-
Assert.That(actions2.actionMaps[0].enabled, Is.False);
432416
Assert.That(playerInput.actions.actionMaps[0].enabled, Is.True);
417+
Assert.That(actions1.actionMaps[0].enabled, Is.False);
433418
}
434419

435420
[Test]
@@ -497,6 +482,22 @@ public void PlayerInput_DuplicatingActions_AssignsNewInstanceToUI()
497482
Assert.That(playerInput2.actions, Is.SameAs(ui2.actionsAsset));
498483
}
499484

485+
[Test]
486+
[Category("PlayerInput")]
487+
public void PlayerInput_ActionFromCodeGeneratedActionIsTheSameBeingReferenced()
488+
{
489+
var go = new GameObject();
490+
491+
var playerInput = go.AddComponent<PlayerInput>();
492+
var ui = go.AddComponent<InputSystemUIInputModule>();
493+
var defaultActions = new DefaultInputActions();
494+
495+
playerInput.uiInputModule = ui;
496+
playerInput.actions = defaultActions.asset;
497+
498+
Assert.That(defaultActions.UI.Submit == playerInput.actions.FindAction("Submit"), Is.True);
499+
}
500+
500501
[Test]
501502
[Category("PlayerInput")]
502503
public void PlayerInput_CanPassivateAndReactivateInputBySendingMessages()

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ however, it has to be formatted properly to pass verification tests.
3131
- Fixed PlayerInput component automatically switching away from the default ActionMap set to 'None'.
3232
- Fixed a console error being shown when targeting visionOS builds in 2022.3.
3333
- Fixed a Tap Interaction issue with analog controls. The Tap interaction would keep re-starting after timeout. [ISXB-627](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-627)
34+
- Fixed PlayerInput component not working with C# Wrappers (ISXB-1535). This reverted changes done to fix [ISXB-920](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-920) but users can now fix it themselves.
3435

3536
## [1.14.0] - 2025-03-20
3637

0 commit comments

Comments
 (0)