Search Unity

Rebinding using PerformInteractiveRebinding

Discussion in 'Input System' started by Nixaan, Mar 1, 2019.

  1. Nixaan

    Nixaan

    Joined:
    May 30, 2013
    Posts:
    118
    Hi,
    I'm trying to figure out how to rebind actions using RebindingOperation/PerformInteractiveRebinding from InputActionRebindingExtensions.
    As I understand it, calling PerformInteractiveRebinding().Start() overrides the whole action. But what I'm trying to achieve is rebinding of only one of several bindings in the action.
    Say an action with 2 bindings, one keyboard key and one gamepad button. I'm looking for a way to detect what device was interacted and update/override only the corresponding binding in the action (or continue waiting until a device existing in the bindings was interacted). Or alternatively specify in advance what device will be rebinded and wait for an input from that device to update/override only the appropriate binding. As last option a different way (not related to PerformInteractiveRebinding) to detect any interaction per device and apply it to the corresponding binding would do too.
     
    Last edited: Mar 2, 2019
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Sorry for the massive lag here...

    There's several ways you can restrict which binding the rebind applies to.

    Code (CSharp):
    1. // Restrict it to the Nth binding on the action.
    2. operation.WithTargetBinding(1);
    3.  
    4. // Restrict it to a "binding group". Binding groups can be freely
    5. // assigned and a binding may have multiple groups. Usually,
    6. // the groups correspond to the control schemes you have set up.
    7. operation.WithBindingGroup("Gamepad");
    8.  
    9. // Completely take over how the override is applied. This
    10. // bypasses the default logic for writing out the new binding path.
    11. operation.OnApplyBinding(
    12.     (op, path) =>
    13.     {
    14.         Debug.Log("Newly selected control path is " + path);
    15.  
    16.         // If you want to detect the device that was used and
    17.         // decide based on that, you could do so here.
    18.         var device = InputControlPath.TryGetDeviceLayout(path);
    19.         //...
    20.     }
     
    ammarajam08 likes this.
  3. Nixaan

    Nixaan

    Joined:
    May 30, 2013
    Posts:
    118
    Should the string passed to WithBindingGroup("groupName") be the exact name of the scheme? It doesn't seem to recognize the name, i.e. no matter what string is passed no action/binding is overridden.
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    The name will usually correspond to the name of the control scheme.

    To make extra sure, it's possible to look up the group dynamically by fetching the control scheme from the asset by name and querying its binding group. Or you can check in the .inputactions file what it says under "bindingGroup" for the control scheme.
     
  5. Nixaan

    Nixaan

    Joined:
    May 30, 2013
    Posts:
    118
    Thanks for the help, I found the problem. The schemes/groups somehow ended in a corrupted state.

    On a side note: I don't know if it is the intended behaviour but on scheme rename the bindingGroup remains with the original name the scheme was created, i.e. renaming a scheme doesn't rename the bindingGroup.

    Anyway, the problem was than neither assetGeneratedClass.NameScheme.name nor assetGeneratedClass.NameScheme.bindingGroup were "accepted as valid" by WithBindingGroup(groupName). Renaming the schemes didn't fix the problem, but after deleting and creating them anew they were working.

    What I noticed with the corrupted schemes is that in the asset window after selecting SchemeName -> All Devices sometimes no bindings(i.e. empty actions) were shown even when every single binding was assigned to a scheme. On selecting SchemeName -> SpecificDevice the bindings for that device were shown fine.
     
    Last edited: Mar 14, 2019
  6. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Good catch. Think it makes sense for it to update the binding group on a rename. Either that or schemes should get stable IDs like maps, actions, and bindings. Made a note.

    Ok, definitely sounds buggy. The action editor recently received a pretty big rewrite/refactor that addressed a range of issues and extend to the paths covering the functionality above. The changes should be in the next package. My hope is this has been resolved as a side-effect.
     
    AlexDuff likes this.