Skip to content

Commit 1b40c51

Browse files
committed
Change to disable PWA actions when using it in PlayerInput
1 parent 538a7fa commit 1b40c51

File tree

4 files changed

+90
-23
lines changed

4 files changed

+90
-23
lines changed

Assets/Tests/InputSystem/CoreTests_ProjectWideActions.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using NUnit.Framework;
55
using UnityEngine.InputSystem;
66
using UnityEngine.TestTools;
7+
using UnityEngine;
78

89
#if UNITY_EDITOR
910
using System.IO;
1011
using UnityEditor;
11-
using UnityEngine;
1212
using UnityEngine.InputSystem.Editor;
1313
#endif // UNITY_EDITOR
1414

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

103103
// In editor play-mode we may as well verify that the asset has the expected name
104-
#if UNITY_EDITOR
104+
#if UNITY_EDITOR
105105
var expectedName = InputActionImporter.NameFromAssetPath(AssetDatabase.GetAssetPath(InputSystem.actions));
106106
Assert.That(InputSystem.actions.name, Is.EqualTo(expectedName));
107-
#endif
107+
#endif
108108
}
109109

110110
[Test]
@@ -140,6 +140,51 @@ public void ProjectWideActions_AppearInEnabledActions()
140140

141141
// TODO Modifying the actions object after being assigned should also enable newly added actions?
142142
}
143+
144+
[Category(TestCategory)]
145+
[Test]
146+
// Test that will make sure that a PlayerInput component using project wide actions will:
147+
// - Have the only default action map set enabled, and all others disabled.
148+
// - Have all action maps of project-wide actions disabled, if there's no default action map selected.
149+
[TestCase("Player", true)]
150+
[TestCase(null, false)]
151+
public void ProjectWideActions_AreDisabledWithPlayerInput(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+
#if UNITY_EDITOR
164+
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingEditMode);
165+
#endif
166+
167+
// This makes sure to call PlayerInput.OnEnable()
168+
go.SetActive(true);
169+
170+
Assert.That(ReferenceEquals(playerInput.actions, InputSystem.actions));
171+
Assert.That(playerInput.actions.enabled, Is.EqualTo(expectedResult));
172+
173+
#if UNITY_EDITOR
174+
// This mimics the behavior of entering play-mode, which will check if InputSystem.EnableActions() should
175+
// be called.
176+
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredPlayMode);
177+
#endif
178+
179+
if (actionMapName != null)
180+
Assert.That(playerInput.currentActionMap.enabled, Is.EqualTo(expectedResult));
181+
182+
Assert.That(InputSystem.actions.FindActionMap("Player").enabled, Is.EqualTo(expectedResult));
183+
Assert.That(InputSystem.actions.FindActionMap("UI").enabled, Is.False);
184+
185+
//NOTE: Asset actions will be considered enabled even if a single action map is enabled
186+
Assert.That(playerInput.actions.enabled, Is.EqualTo(expectedResult));
187+
}
143188
}
144189

145190
#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/InputSystem/InputSystem.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,6 +3038,19 @@ private static void EnableActions()
30383038
actions.Enable();
30393039
}
30403040

3041+
/// <summary>
3042+
/// Determines whether project-wide actions are automatically enabled after entering Play Mode.
3043+
/// </summary>
3044+
/// <remarks>
3045+
/// This property is set to <c>true</c> by default.
3046+
/// </remarks>
3047+
internal static bool m_EnableActionsAfterEnterPlayMode = true;
3048+
3049+
internal static void AllowEnableActionsAfterEnterPlayMode(bool enabled)
3050+
{
3051+
m_EnableActionsAfterEnterPlayMode = enabled;
3052+
}
3053+
30413054
private static void DisableActions(bool triggerSetupChanged = false)
30423055
{
30433056
// Make sure project wide input actions are disabled
@@ -3408,7 +3421,6 @@ public static int ListEnabledActions(List<InputAction> actions)
34083421

34093422
#endregion
34103423

3411-
34123424
/// <summary>
34133425
/// The current version of the input system package.
34143426
/// </summary>
@@ -3655,7 +3667,7 @@ internal static void OnPlayModeChange(PlayModeStateChange change)
36553667
// Don't enable actions if some are already enabled.
36563668
// This is useful when you just want specific action maps to be enabled, but not all
36573669
// action maps.
3658-
if (!InputSystem.actions.enabled)
3670+
if (m_EnableActionsAfterEnterPlayMode)
36593671
EnableActions();
36603672

36613673
#endif // UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,15 @@ public void ActivateInput()
954954

955955
m_InputActive = true;
956956

957+
// Disable project-wide actions if they are being used by this PlayerInput.
958+
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
959+
if (m_Actions != null && m_Actions == InputSystem.actions)
960+
{
961+
InputSystem.AllowEnableActionsAfterEnterPlayMode(false);
962+
InputSystem.actions.Disable();
963+
}
964+
#endif
965+
957966
// If we have no current action map but there's a default
958967
// action map, make it current.
959968
if (m_CurrentActionMap == null && m_Actions != null && !string.IsNullOrEmpty(m_DefaultActionMap))

0 commit comments

Comments
 (0)