Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I change controls at runtime?

Discussion in 'Input System' started by Marscaleb, Jul 7, 2020.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,036
    I'm trying to set up a system to allow plays to change their controls.

    I've figured out how to access a particular action via code:
    gameObject.GetComponent<PlayerInput>().actions.FindAction("Jump")
    and there are some functions from there to change bindings, but I can't find any documentation on the "change binding" methods, only the "add binding" methods.

    Furthermore, even if I were to try using the "Add binding" methods, I don't see how to read from any user input.
    For right now, let's just assume we are only working with a gamepad. If the player changes a button, I want them to be able to (when prompted) just press whatever button they want and have it be assigned. So ideally, I want to be able change the binding to whatever is specified by player input.

    Additionally, I am going to need to be able to read what button is assigned to an action so I can have it displayed in the menu.

    Any thoughts here?
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Code (CSharp):
    1. GetComponent<PlayerInput>().actions["jump"].ApplyBindingOverride("<Gamepad>/buttonSouth");
    Code (CSharp):
    1. GetComponent<PlayerInput>().actions["jump"].GetBindingDisplayString();
     
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,036
    So I notice that the string that gets returned when I use GetBindingDisplayString is returning values like "A" and "X" (or "Press A" and "Press X") instead of the universal codes like "Button South" and "Button West."
    Are these names dependent on what kind of controller I'm using, or will they always return these values? Like if I used a Playstation controller will the buttons still return as "A" and "B" or will they be "Cross" and "Circle"? If I had some generic gamepad would they be "button 1" and so forth?
    As nice as it is to get a string name for these buttons, I will want to display icons in my menu. If the back-end isn't consistent with what names it provides me, then we've got a bit of a problem.