Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Assets/Tests/InputSystem/Plugins/PlayerInputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,39 @@ public void PlayerInput_CanDisableAfterAssigningAction_WithControlSchemesAndInte
player.SetActive(false); // Should cause full rebinding and not assert
}

[Test]
[Category("PlayerInput")]
public void PlayerInput_DelegatesAreUpdate_WhenActionMapAddedAfterAssignment()
{
var gamepad = InputSystem.AddDevice<Gamepad>();

var go = new GameObject();
var listener = go.AddComponent<MessageListener>();
var playerInput = go.AddComponent<PlayerInput>();
playerInput.defaultActionMap = "Other";
var actionAsset = InputActionAsset.FromJson(kActions);
playerInput.actions = actionAsset;

// Disable the asset while adding another action map to it as none
// of the actions in the asset can be enabled during modification
//
actionAsset.Disable();
var keyboard = InputSystem.AddDevice<Keyboard>();
var newActionMap = actionAsset.AddActionMap("NewMap");
var newAction = newActionMap.AddAction("NewAction");
newAction.AddBinding("<Keyboard>/k", groups: "Keyboard");
actionAsset.AddControlScheme("Keyboard").WithRequiredDevice<Keyboard>();
actionAsset.Enable();

playerInput.currentActionMap = newActionMap;
playerInput.ActivateInput();
listener.messages.Clear();

Press(keyboard.kKey);

Assert.That(listener.messages, Has.Exactly(1).With.Property("name").EqualTo("OnNewAction"));
}

private struct Message : IEquatable<Message>
{
public string name { get; set; }
Expand Down Expand Up @@ -2477,6 +2510,11 @@ public void OnOtherAction(InputValue value)
messages?.Add(new Message { name = "OnOtherAction", value = value.Get<float>() });
}

public void OnNewAction(InputValue value)
{
messages?.Add(new Message { name = "OnNewAction", value = value.Get<float>() });
}

// ReSharper disable once UnusedMember.Local
public void OnActionWithSpaces(InputValue value)
{
Expand Down
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ however, it has to be formatted properly to pass verification tests.
- Fixed an issue in `Samples/Visualizers/GamepadVisualizer.unity` sample where the visualization wouldn't handle device disconnects or current device changes properly (ISXB-1243).
- Fixed an issue when displaying Serialized InputAction's Processor properties inside the Inspector window. [ISXB-1269](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1269)
- Fixed an issue with default device selection when adding new Control Scheme.
- Fixed an issue where action map delegates were not updated when the asset already assigned to the PlayerInput component were changed [ISXB-711](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-711).

### Changed
- Added back the InputManager to InputSystem project-wide asset migration code with performance improvement (ISX-2086).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,8 @@ public TDevice GetDevice<TDevice>()
/// </example>
public void ActivateInput()
{
UpdateDelegates();

m_InputActive = true;

// If we have no current action map but there's a default
Expand All @@ -960,6 +962,31 @@ public void ActivateInput()
m_CurrentActionMap?.Enable();
}

// Users can add and remove actions maps *after* assigning an InputActionAsset to the PlayerInput component.
// This ensures "actionTriggered" delegates are assigned for new maps (case isxb-711)
//
private int m_AllMapsHashCode = 0;
private void UpdateDelegates()
{
if (m_Actions == null)
{
m_AllMapsHashCode = 0;
return;
}

int allMapsHashCode = 0;
foreach (var actionMap in m_Actions.actionMaps)
{
allMapsHashCode ^= actionMap.GetHashCode();
}
if (m_AllMapsHashCode != allMapsHashCode)
{
InstallOnActionTriggeredHook();
CacheMessageNames();
m_AllMapsHashCode = allMapsHashCode;
}
}

/// <summary>
/// Disable input on the player, by disabling the current action map
/// </summary>
Expand Down Expand Up @@ -1515,6 +1542,8 @@ private void CacheMessageNames()

private void ClearCaches()
{
if (m_ActionMessageNames != null)
m_ActionMessageNames.Clear();
}

/// <summary>
Expand Down
Loading