Search Unity

Resolved checking device compatibility

Discussion in 'Input System' started by Sauron1234, Apr 28, 2022.

  1. Sauron1234

    Sauron1234

    Joined:
    Sep 22, 2014
    Posts:
    29
    InputDevice gamepad = InputSystem.AddDevice<Gamepad>();
    inputActionMap.IsUsableWith(gamepad);

    the documentation shows that this is how to check if an input device is able to function with an InputActionMap. But IsUsableWith does not exist when I compile!!

    ----------------------------
    what I want to do is check if the device is compatible with my InputAction binding... BUT only for a specific deivice.

    InputControlPath.TryFindControl(device, binding.effectivePath);
    --> this will show me if the binding will function on a specific device and can alert the user if the device is imcompatible with my Input System.
    BUT I want to make sure the device being checked is the same.
    so like this:
    if( device.type = binding.device.type )

    since the binding "<Keyboard>/space" should NOT work with a gamepad so no check is needed to be made.

    so how can I tell the what device is the binding for??
     
  2. Sauron1234

    Sauron1234

    Joined:
    Sep 22, 2014
    Posts:
    29
    I managed to make it work like this:



    foreach (InputBinding binding in inputAction.bindings)
    {
    if (Keyboard.current != null && InputControlPath.TryGetDeviceLayout(binding.effectivePath) == "Keyboard")
    {
    var control = InputControlPath.TryFindControl(Keyboard.current, binding.effectivePath);
    if (control == null)
    {
    //error code here//
    Debug.Log("keyboard key is bound to an input that does not exist on the device!!");
    break;
    }
    }
    }


    can get action from the asset:
    InputAction inputAction =InputActionAsset.actions[0];
    or make a new action yourself and add bindings:
    InputAction inputAction = new InputAction();
    inputAction.AddBinding("<Mouse>/leftButton");
    inputAction.AddBinding("<Keyboard>/anyKeyYouWantHere");
     
  3. Sauron1234

    Sauron1234

    Joined:
    Sep 22, 2014
    Posts:
    29
    for gamepads example:


    foreach (InputBinding binding in inputAction.bindings)
    {
    if (Gamepad.current != null && InputControlPath.TryGetDeviceLayout(binding.effectivePath) == "Gamepad")
    {
    var control = InputControlPath.TryFindControl(Gamepad.current, binding.effectivePath);
    if (control == null)
    {
    //error code here//
    Debug.Log("Gamepad button is bound to an input that does not exist on the device!!");
    break;
    }
    }
    }