Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Access Input Action Properties via script...

Discussion in 'Input System' started by SilverFang180882, Apr 1, 2022.

  1. SilverFang180882

    SilverFang180882

    Joined:
    Mar 30, 2020
    Posts:
    35
    Hello,

    Can someone help me figure out the following syntax for accessing Input Action properties via script? I just want to be able to access a certain binding and display its name in a text field in the scene.

    For example,
    I have an input that has 3 bindings: "Keyboard/Space", "Gamepad/ButtonNorth" and "JoyStick/Trigger"

    I want to be able to display one of the three bindings in a text field:
    inputTxt1.text = [Whatever the syntax is to display Space]
    inputTxt2.text = [Whatever the syntax is to display Button North]
    inputTxt3.text = [Whatever the syntax is to display Trigger]

    The thing is, I'm a bit lost on any syntax relating to accessing the Input Action properties, as I'm quite new with using the new input system. All tutorials I've found on the subject seem to skip this and use methods that are very different from my set-up (usually to display the input through a function, only when it's pressed), so I was hoping there would be someone here who can help me figure out how to access these properties manually.

    Thanks in advance.
     
  2. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    First step is to grab the action you want to generate a display string for. Depends a bit on what setup you have there.

    Code (CSharp):
    1. // PlayerInput.
    2. var action = playerInput.actions["actionName"];
    3.  
    4. // With "Generate C# Class".
    5. var action = myInput.actionName;
    After that you can use
    GetBindingDisplayString()
    turn get strings from the action.

    Code (CSharp):
    1. // Generate a display string for whatever active bindings
    2. // there are on the action.
    3. var str = action.GetBindingDisplayString();
    4.  
    5. // Generate a display string for the Nth binding.
    6. var str = action.GetBindingDisplayString(1);
    There's a a sample that comes with the package called "In-Game Hints" which may be helpful to look at here.
     
    SilverFang180882 likes this.
  3. SilverFang180882

    SilverFang180882

    Joined:
    Mar 30, 2020
    Posts:
    35
    Ah ok, got it. Thanks (I had to tweak it a lot though, as my code doesn't like me using "var", nor did it like me assigning the InputAction field separately from the long text string to access the bindings. I ended up having to do this:
    Code (CSharp):
    1. shoot1Txt.text = playerControls.GetComponent<PlayerInput>().actions["Shoot"].GetBindingDisplayString(0);       //Display the current Shoot 1 button name
    Incidentally, how do you use these properties within conditions? For example, you set the "GetBindingDisplayString(X)" to a number that's out of range from the number of bindings, but to stop an error, you put it in a condition like this:

    Code (CSharp):
    1. if (playerControls.GetComponent<PlayerInput>().actions["Shoot"].GetBinding(4) != null)
    2.     shoot3Txt.text = playerControls.GetComponent<PlayerInput>().actions["Shoot"].GetBindingDisplayString(4);
    3. else
    4.     shoot3Txt.text = "None";
    (If that makes any sense--the above statement does bring up errors, you see, as I'm not sure how to correctly write the condition)

    On another note, when it came to the gamepad bindings, it always displays the X-Box button name (i.e. button south is A, button north is Y, etc). Is it possible to make it display just "Button North" or "Button South" (or is this more easily achieved with a condition as above)?

    Thanks once again though, you certainly helped me get past that initial step.
     
  4. Stephanommg

    Stephanommg

    Joined:
    Aug 18, 2014
    Posts:
    72
    There is no field with the action name in the generated c# class for me.
     
  5. SilverFang180882

    SilverFang180882

    Joined:
    Mar 30, 2020
    Posts:
    35
    "actionName" was just an example he used. You change that to whatever name you gave the input command in your specific code (i.e. "Jump", "MoveRight", etc). The example I used in my one was "Shoot".