Search Unity

Correct way of switching devices

Discussion in 'Input System' started by Tyndareus, Mar 24, 2020.

  1. Tyndareus

    Tyndareus

    Joined:
    Aug 31, 2018
    Posts:
    37
    Been looking for quite a while for this information but all I have is errors from the results, firstly the original issue was that the controller wouldn't be detected when connecting after running the app, that seems to be resolved simply by adding multiple schemes and allowing it to auto-switch.

    Now however there is a need to display the available devices and be able to switch between them whenever (by some UI event or whatever). So to do that I currently need to use the InputSystem.onDeviceChanged so that I can notify the UI that theres an updated device list and it would go away and do that correctly. With a fresh instance of Unity (as expected with a test case) plugging in the device and using onDeviceChanged to update the control scheme breaks, it just gives the "Invalid User" error; thats only with the Added state, Reconnected works fine.

    What is the correct way of handling this?
    (I am assuming that when this is setup passing the device back over from the UI to say "switch to this scheme" should work)

    - Edit, the desired effect afterwards doesn't work so the input itself has an issue with devices being added after launch?
    Actual error comes from UnpairDevices
    Code (csharp):
    1. InvalidOperationException: Invalid user
    2. UnityEngine.InputSystem.Users.InputUser.get_index () (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.6/InputSystem/Plugins/Users/InputUser.cs:88)
    3. UnityEngine.InputSystem.Users.InputUser.UnpairDevices () (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.6/InputSystem/Plugins/Users/InputUser.cs:627)
    4. UnityEngine.InputSystem.PlayerInput.SwitchCurrentControlScheme (System.String controlScheme, UnityEngine.InputSystem.InputDevice[] devices) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.6/InputSystem/Plugins/PlayerInput/PlayerInput.cs:866)
    My current workaround is to destroy the PlayerInput and recreate it so it has new bindings
     
    Last edited: Mar 24, 2020
    emredesu likes this.
  2. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    InputUser.PerformPairingWithDevice(device, InputUser.all[0]);