Search Unity

Feedback Optional layouts in control schemes & virtual mouse use

Discussion in 'Input System' started by Zuntatos, Jan 1, 2023.

  1. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    Just documenting a problem I ran into with the new input system & what resolved it. Hopefully future people googling for this issue find this useful.

    The problem came when I needed the Virtual Mouse system to work after opening the inventory in my game, which is not really stick-navigation-compatible. The Virtual Mouse would move visually, the debug data on the device looked OK, but it would not trigger anything in the EventSystems, nor would it trigger Mouse actions in the new input system. I was swapping the active action mapping to a specific UI one to prevent duplicate gamepad/virtual-mouse inputs (so that shouldn't be an issue).

    Adding the virtual mouse as a required layout to the gamepad control scheme makes it not work when the virtual mouse isn't active (of course).
    Making it an optional layout to the gamepad control scheme didn't make the virtual mouse work.

    You can move around your virtual mouse as you want, but the InputPlayer wouldn't update the current scheme to add the <optional> virtual mouse to the current scheme. Except if you mess around enough, swapping devices a lot, and sometimes it would stick - getting both devices paired (virtual mouse + gamepad) and then the UI would work for me as I wanted it to.

    The solution for me was to run some code in Update() whenever I've got the VirtualMouseInput enabled:

    Code (CSharp):
    1. InputUser? pairUser = InputUser.FindUserPairedToDevice(mouseInput.virtualMouse);
    2. if (!pairUser.HasValue)
    3. {
    4.     InputUser.PerformPairingWithDevice(mouseInput.virtualMouse, InputUser.all[0], InputUserPairingOptions.UnpairCurrentDevicesFromUser);
    5. }
    This force pairs the virtual mouse to the current user (assuming no splitscreen stuff), after which it correctly starts using the "virtual mouse + gamepad" control scheme. And then it feeds it on and on into all the other systems and it works as intended.

    I'm not sure whether something along this line of code should be builtin to the VirtualMouseInput behaviour on enabling it, but this was quite a counter-intuitive problem. I didn't run into anything about this specific problem googling around & in the docs. My general feeling is that the docs have a high-level quickstart & basic samples, in addition to a very low-level "DIY" reference for recreating a higher level system, but sort-of-mid-level issues like this seem to be missing helpful docs.