OK, correction. I’ve just discovered that you don’t need any third-party plugin to support joysticks after all!
It turns out that Input.GetKey (and GetKeyDown/GetKeyUp) are not just for keyboard keys. The KeyCode enum includes a gazillion entries for joystick buttons, too. And if you ask for the state of one of these, it’ll return true if that joystick button is down.
It does this even when you hot-swap your joystick/gamepad while the game is running (!). This didn’t work in the past, and we are unable to find any release notes about it, but it certainly works in our testing today (on both Mac and Windows).
Moreover, the Input.GetJoystickNames method returns decent human-readable names for all the joysticks that are plugged in. And this too seems to update instantly when you swap devices in or out.
The only fly in the ointment is analog axes and D-pads. These don’t appear to respond to GetKey. However, there are only so many of them… if you install and set up Rewired, it defines a ton of virtual axes in your Input settings, corresponding to basically all possible joystick axes. I think you’d be safe if you did this for just, say, 4 axes for each of joysticks 1-8 (a total of 32 virtual axes). And then you can check each of those to see what’s pressed.
Here’s a quick-and-dirty demo of how you can scan for keys/joystick buttons, as well as axis (assuming you have those virtual axes set up).
void Update() {
// Check all the things!
// (Iterating over all KeyCodes.)
foreach (var value in (KeyCode[])Enum.GetValues(typeof(KeyCode))) {
if (Input.GetKey(value)) {
buttonFeedback.text = value.ToString();
}
}
// Check all the virtual axes, since these seem to be the only way to get to d-pads/joysticks.
for (int joynum = 1; joynum<=8; joynum++) {
for (int axisnum=1; axisnum<=4; axisnum++) {
string virtAxisName = string.Format("Joy{0}Axis{1}", joynum, axisnum);
float value = Input.GetAxisRaw(virtAxisName);
if (Mathf.Abs(value) > 0.1f) buttonFeedback.text = virtAxisName + "=" + value;
}
}
joystickText.text = "Joysticks:\n" + string.Join("\n", Input.GetJoystickNames());
}
So, you could make your own configurable input system by just doing this sort of scan while asking the user what key/axis they want to use, remembering the KeyCode or axis that you find, and saving that to prefs. Then just check that same key/axis during your game.