Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How can I register a method to an individual input user without the player input component?

Discussion in 'Input System' started by gigazelle, May 12, 2024.

  1. gigazelle

    gigazelle

    Joined:
    May 6, 2017
    Posts:
    14
    I am currently developing a split screen game. I would like to use the workflow that allows me to interface with the input actions asset:

    https://docs.unity3d.com/Packages/com.unity.inputsystem@1.8/manual/Workflow-Actions.html

    One of the advantages of this workflow is that I have full control over input users, when they join, and how they act. Compare this to the player input manager, which requires both a player input manager script, and multiple player input scripts. As mentioned in the docs for this workflow, it acts as a "black box", and I would prefer to not go that route.

    Anyways, I have things working up to a certain point, but I cannot figure out how to map a method to run when input from a specific device is performed.

    Code (CSharp):
    1. // Create input user and pair it with the desired device
    2. InputUser player = InputUser.PerformPairingWithDevice(InputSystem.GetDevice<Keyboard>());
    3.  
    4. // Tie the input action asset to the input user
    5. player.AssociateActionsWithUser(inputActions);
    6.  
    7. // Activate a control scheme for the device so it knows to use the right controls
    8. player.ActivateControlScheme("Keyboard");
    9.  
    10. // Register a C# method to run when a button is pressed
    11. player.actions.???
    When interfacing with the input action asset directly, it's pretty straightforward:

    Code (CSharp):
    1. // Player is the action map and jump is the action
    2. inputActions.Player.Jump.performed += x => { /* Do stuff */ };
    3.  
    4. // Alternatively if I want to assign a proper method to it
    5. inputActions.Player.Jump.performed += JumpMethod;
    6. void JumpMethod(InputAction.CallbackContext x) {
    7.   // Jump logic here
    8. }
    However, that does not allow any differentiation between input users. Works great for single player games, though.

    What am I missing here? Is what I'm asking for possible without having to use the player input component? I would really prefer to avoid using the components if possible and stick with my own scripts.
     
  2. Abdo-Reda

    Abdo-Reda

    Joined:
    Oct 8, 2015
    Posts:
    18
    Well, I tried the following and it worked. Basically, you will do the same logic. But since you have a list of actions in your input user, you will have to:
    • Either loop over the action or filter using LINQ to get the InputAction you want.
    • Enable that input action (I think they are disabled by default).
    • Then listen to .performed or do anything with that InputAction.


    Code (CSharp):
    1.  var jumpInputAction = player.actions.First(action => action.name == "jump");
    2. Debug.Log($"found input action {jumpInputAction.id}");
    3. jumpInputAction.Enable();
    4. jumpInputAction.performed += Jump;
    I am not entirely sure if this is entirely performant. But, I think its better not to worry about performance until you face issues, then you open the profiler and check where the problem is from :)