Search Unity

How to recreate the PlayerInput class ?

Discussion in 'Input System' started by anthonov, Oct 11, 2020.

  1. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    Doc says PlayerInput is built on top of the new input system and can be rewrited.
    So, for some reason Im trying my best to recreate it, without success.
    What I did so far is a monoBehaviour class with one "InputActionAsset" instance, and one "InputUser" instance.
    Then I associate the InputUser instance with the InputActionAsset instance with the "AssociateActionWithUser()" methode, then activate one sheme.
    I can't go further ; I have an error when starting:
    "InvalidOperationException: Invalid user"

    Im trying this because the PlayerInput class does not give me the control I expect for a local multiplayer game.
    (I can't navigate through it with original actions/map names like with the class generated by the InputActionAsset)

    Now I will use string parameter to use the PlayerInput actions/map because it seems to hard to rewrite, but Im still curious to see if it is possible.
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    InputUser
    is a struct that acts like a handle. To create a user, either call
    InputUser.CreateUserWithoutPairedDevices()
    or use
    InputUser.PerformPairingWithDevice()
    .

    Code (CSharp):
    1. // Create a new InputUser that "owns" the keyboard and the mouse.
    2. var user = InputUser.PerformPairingWithDevice(Keyboard.current);
    3. InputUser.PerformPairingWithDevice(Mouse.current, user: user);
    4.  
    5. // Give the user a set of actions and choose a control scheme.
    6. // (assuming a C# class generated with "Generate C# Class" here)
    7. var actions = new MyGeneratedActions();
    8. user.AssociateActionWithUser(actions);
    9. user.ActivateControlScheme(actions.GamepadScheme);
     
  3. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    thanks,
    I found that I can override actions asset instance directly on PlayerInput, so now I can activate or deactivate actions by their name easily.