0
\$\begingroup\$

I'm creating a key rebinding system for my project. It detects when a key (including controller buttons) or mouse button is pressed and, when that input is detected, it takes the name of the key or button as a string and feeds it to either Input.GetKey or Input.GetMouseButton (the method can determine whether the input detected is a key or a mouse button). Currently, the system outputs this same string to a text field in the rebinding menu, but I want to change this to use images instead. I could do this by directly associating every single string with an image, like so:

string keyBind;
Image imageTarget;
Texture2D bindImageW;
Texture2D bindImageS;
Texture2D bindImageA;
Texture2D bindImageD;

switch (keyBind)
{
    case ("w"):
    {
        imageTarget.sprite = bindImageW;
        break;
    }
        case ("s"):
    {
        imageTarget.sprite = bindImageS;
        break;
    }
        case ("a"):
    {
        imageTarget.sprite = bindImageA;
        break;
    }
        case ("d"):
    {
        imageTarget.sprite = bindImageD;
        break;
    }
}

Etc. However, this method of manually associating the name of every single key on the keyboard, all mouse buttons, and about a dozen controller buttons seems very clunky and tedious. Is there a cleaner and/or automated method to "pair" each of these key name strings with an image file, and then set that image in the UI?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ The rebinding portion seems like something that would be more efficient to do with the new Input System package, rather than the legacy Input Manager. For the image association, presumably you considered building a dictionary on start-up, or at edit time as a list/array that can be serialized? You could use the name of the sprite to select the input to map it to, if you're consistent with your naming convention. \$\endgroup\$
    – DMGregory
    Aug 1 at 19:56

0

You must log in to answer this question.

Browse other questions tagged .