Search Unity

Question Get composite binding from part-of-composite binding (or vice versa)

Discussion in 'Input System' started by achimmihca, Jan 3, 2022.

  1. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    283
    Given an InputBinding where isPartOfComposite is true. How to find the composite InputBinding that uses it (the one where isComposite is true)?
    I would have expected the data structure to have a list of child bindings or parent field, but did not find any.

    The other way around would also work for me, i.e. find parts-of-composite bindings given a composite binding.
     
    Last edited: Jan 3, 2022
    marzuqh and DoN2kcz like this.
  2. marzuqh

    marzuqh

    Joined:
    Jun 9, 2021
    Posts:
    1
    Posting this in case it's helpful. Not a robust solution.

    It's a hacky function that gets a composite binding for the currentControlScheme in PlayerInput. I use it in the RebindingUIPrefab of the new Unity Input package to update what binding is display.

    The relevant section is in bold. If you've found an action via it's bindingIndex and it isPartOfComposite, you can try bindingIndex - 1 to get the composite of parts. It's worked so far for me.

    Code (CSharp):
    1.         void SetBindingToNewControlScheme()
    2.         {
    3.             var action = playerInput.actions.FindAction(m_Action.name);
    4.  
    5.             // if composite action / can't use binding index or need to specify
    6.             int bindingIndex = action.GetBindingIndex(group: playerInput.currentControlScheme);
    7.  
    8.             if (action.bindings[bindingIndex].isPartOfComposite)
    9.             {
    10.                 // hard coded logic - assumes that if you found a part of a composite, that it's the first one.
    11.                 // And that the one preceeding it, must be the 'Composite head' that contains the parts
    12.                 bindingId = action.bindings[bindingIndex-1].id.ToString();
    13.             }
    14.             else { bindingId = action.bindings[bindingIndex].id.ToString(); } // if not a composite, bindingId can just be updated
    15.         }
     
    Kondziu2504 and Ensortainment like this.