Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Updating device

Discussion in 'Input System' started by Tyndareus, Oct 22, 2019.

  1. Tyndareus

    Tyndareus

    Joined:
    Aug 31, 2018
    Posts:
    29
    How can you swap devices?
    If I spawn a PlayerInput and let it use the keyboard, how do I extend that in future so that if they connect a controller, i can add to that ReadOnly array, there appears to be nothing for it other than destroying the PlayerInput and recreating it with the new device.
     
  2. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    235
    Use control scheme, set up one with a controller, then you can switch to it
     
  3. Tyndareus

    Tyndareus

    Joined:
    Aug 31, 2018
    Posts:
    29
    Even if the devices that are 'used' by the PlayerInput doesn't change or doesn't update so it still polls keyboard?

    So again, the question was about updating the devices, not the scheme.
    How can it use the controller scheme if its locked out of using that device?
     
    Last edited: Oct 22, 2019
  4. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    235
    Don't really understand what you're trying to do, but using PlayerInput you don't have to care about device pairing, it handles everything for you
     
  5. Tyndareus

    Tyndareus

    Joined:
    Aug 31, 2018
    Posts:
    29
    If only the player input did that, I wouldn't be asking this question.

    I instantiate a player with the player input variant of instantiate, passing Gamepad.all to the final device parameter.
    That will assign keyboard/mouse if the controller isn't plugged in, obviously after plugging in the controller unity knows the device has been added but the player input is locked out; the device array is read only so i cant forcibly say "This is your new device".
    DeviceRegained and DeviceLost are only applicable to the player input's devices; so if i unplugged my keyboard/mouse it would say the device has been lost or found.

    Edit; I have a temporary solution which doesn't seem to impact performance, using the InputSystem.onDeviceChanged i just reconstruct the PlayerInput with the connected device
     
    Last edited: Oct 22, 2019
  6. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,521
    To get automatic switching, you'll need control schemes. If there's multiple control schemes and only a single PlayerInput, it should switch automatically when a compatible device is plugged in and used (the latter is important; the system won't switch to a device where the user isn't pressing buttons or sticks).

    Note that you can also alter pairing manually at the API level using InputUser. PlayerInput has no wrapper API for that ATM but you can just grab PlayerInput.user and use the various pairing functions that InputUser has (PerformPairingWithDevice, UnpairDevices, etc).
     
  7. Tyndareus

    Tyndareus

    Joined:
    Aug 31, 2018
    Posts:
    29
    I will quickly look at setting a secondary scheme and just letting the user construct with "first available".

    If i was to manage the pairing myself would I need to first unpair the current device and pair the new one? Or would simply pairing overwrite the existing? (I assume it gets put into that InputDevice array though)
     
  8. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,521
    It's up to you.

    Code (CSharp):
    1. // Switch device on a player.
    2. InputUser.PerformPairingWithDevice(
    3.     Keyboard.current,
    4.     user: player.user,
    5.     options: InputUserPairingOptions.UnpairCurrentDevicesFromUser);
    6.  
    7. // Add a device to a player.
    8. InputUser.PerformPairingWithDevice(
    9.     Keyboard.current,
    10.     user: player.user);
     
  9. Tyndareus

    Tyndareus

    Joined:
    Aug 31, 2018
    Posts:
    29
    Ah didn't pay attention to the options.

    I may need to do that route however since despite having two control schemes and all assigned up (keyboard required for one and gamepad required for the other, all of the inputs that are specific to each device separated), the input did not switch. That was just from instantiating the object without passing any devices to it, it would however still spawn with the keyboard and mouse.
     
  10. TimHeijden

    TimHeijden

    Joined:
    Mar 5, 2015
    Posts:
    29
    I'm actually just did something exactly like this right now. One thing I found a bit tricky to figure out was that AFTER performing this pairing, I also needed to manually trigger the control scheme being updated using:
    Code (CSharp):
    1.  
    2. player.user.ActivateControlScheme(null);
    3.  
    I can see the logic behind this in hindsight, but I think it might be good to point out somewhere in documentation. (Or did I miss this somewhere?) Note that I haven't really looked into controlSchemes at all since I only want to use the "default' schemes until my game is further along.


    EDIT: Actually, it looks like while this works when I add a controller to a KBM player, when I add a mouse to a controller player it doesn't work. (Though adding a keyboard DOES work)

    So I'm confused now!
     
    Last edited: Nov 2, 2019
unityunity