Search Unity

Get the binding that triggered said event.

Discussion in 'Input System' started by quint_unity, Oct 23, 2020.

  1. quint_unity

    quint_unity

    Joined:
    Oct 11, 2020
    Posts:
    13
    Say I have a single action with three different bindings attached to it. Is there a way to retrieve what binding triggered that action? Example below if what I'm thinking.

    Code (CSharp):
    1. public class GameController : MonoBehaviour
    2. {
    3.     public void OnMouseClick(InputAction value)
    4.     {
    5.         Debug.Log(value);
    6.         switch (value)
    7.         {
    8.         case 0:
    9.             Debug.Log("leftClick Event");
    10.             break;
    11.         case 1:
    12.             Debug.Log("rightClick Event");
    13.             break;
    14.         case 2:
    15.             Debug.Log("middleClick Event");
    16.             break;
    17.         }
    18.     }
    19. }
    Capture.PNG
     
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    The Input Action's performed callback hands back an object called InputAction.CallbackContext which contains info about what triggered the action. The callback context has a property called "control" which is a reference to the objects you see in blue in your picture--one "binding" that can trigger the action. "Control" is an InputControl, which has a property called path. Path is a string representation describing the type of hardware input that this InputControl uses, i.e. keyboard/w button or in your case mouse/left button, etc. You can read more about this path structure here.
     
    Toxicbullets10 likes this.
  3. Toxicbullets10

    Toxicbullets10

    Joined:
    Jun 9, 2022
    Posts:
    1
    super helpful thanks, solved alot of confusion
     
    SomeGuy22 likes this.