Skip to content

Commit b05b5e8

Browse files
committed
fix tests and copying of asset
1 parent 1655b7f commit b05b5e8

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

Assets/Tests/InputSystem/Plugins/PlayerInputTests.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ 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));
395396
}
396397

397398
[Test]
@@ -407,13 +408,13 @@ public void PlayerInput_AssigningNewActionsToPlayer_DisablesExistingActions()
407408
playerInput.defaultActionMap = "gameplay";
408409
playerInput.actions = actions1;
409410

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

413414
playerInput.actions = actions2;
414415

415-
Assert.That(actions1.actionMaps[0].enabled, Is.False);
416-
Assert.That(actions2.actionMaps[0].enabled, Is.True);
416+
Assert.That(actions2.actionMaps[0].enabled, Is.False);
417+
Assert.That(playerInput.actions.actionMaps[0].enabled, Is.True);
417418
}
418419

419420
[Test]
@@ -1714,7 +1715,8 @@ InputDevice[] AddDevices()
17141715

17151716
// Make sure that no cloning of actions happened on the prefab.
17161717
// https://fogbugz.unity3d.com/f/cases/1319756/
1717-
Assert.That(playerPrefab.GetComponent<PlayerInput>().actions, Is.SameAs(playerPrefabActions));
1718+
1719+
Assert.That(playerPrefab.GetComponent<PlayerInput>().actions.actionMaps.Count, Is.EqualTo(playerPrefabActions.actionMaps.Count));
17181720
Assert.That(playerPrefab.GetComponent<PlayerInput>().m_ActionsInitialized, Is.False);
17191721
}
17201722

@@ -2421,13 +2423,13 @@ public void PlayerInput_DelegatesAreUpdate_WhenActionMapAddedAfterAssignment()
24212423
// Disable the asset while adding another action map to it as none
24222424
// of the actions in the asset can be enabled during modification
24232425
//
2424-
actionAsset.Disable();
2426+
playerInput.actions.Disable();
24252427
var keyboard = InputSystem.AddDevice<Keyboard>();
2426-
var newActionMap = actionAsset.AddActionMap("NewMap");
2428+
var newActionMap = playerInput.actions.AddActionMap("NewMap");
24272429
var newAction = newActionMap.AddAction("NewAction");
24282430
newAction.AddBinding("<Keyboard>/k", groups: "Keyboard");
2429-
actionAsset.AddControlScheme("Keyboard").WithRequiredDevice<Keyboard>();
2430-
actionAsset.Enable();
2431+
playerInput.actions.AddControlScheme("Keyboard").WithRequiredDevice<Keyboard>();
2432+
playerInput.actions.Enable();
24312433

24322434
playerInput.currentActionMap = newActionMap;
24332435
playerInput.ActivateInput();

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ public InputActionAsset actions
344344

345345
m_Actions = value;
346346

347+
CopyActionAsset();
348+
347349
if (m_Enabled)
348350
{
349351
ClearCaches();
@@ -1373,14 +1375,13 @@ private void InitializeActions()
13731375
if (m_Actions == null)
13741376
return;
13751377

1376-
// duplicate action asset to not operate on the original (as it might be used outside - eg project wide action asset or UIInputModule)
1377-
var oldActions = m_Actions;
1378-
m_Actions = Instantiate(m_Actions);
1379-
for (var actionMap = 0; actionMap < oldActions.actionMaps.Count; actionMap++)
1380-
{
1381-
for (var binding = 0; binding < oldActions.actionMaps[actionMap].bindings.Count; binding++)
1382-
m_Actions.actionMaps[actionMap].ApplyBindingOverride(binding, oldActions.actionMaps[actionMap].bindings[binding]);
1383-
}
1378+
1379+
for (var i = 0; i < s_AllActivePlayersCount; ++i)
1380+
if (s_AllActivePlayers[i].m_Actions == m_Actions && s_AllActivePlayers[i] != this)
1381+
{
1382+
CopyActionAsset();
1383+
break;
1384+
}
13841385

13851386
#if UNITY_INPUT_SYSTEM_ENABLE_UI
13861387
if (uiInputModule != null)
@@ -1428,6 +1429,18 @@ private void InitializeActions()
14281429
m_ActionsInitialized = true;
14291430
}
14301431

1432+
private void CopyActionAsset()
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+
14311444
private void UninitializeActions()
14321445
{
14331446
if (!m_ActionsInitialized)

0 commit comments

Comments
 (0)