I was getting really fed up with the default input binding system in Unity - specifically around having to map tons of different gamepad Axis and buttons for each platform. It's ridiculous. You end up having to do Application.platform
checks all over the place and it's super disgusting.
Anyway, I created an abstraction over some of the input manager. When you call GetAxis
it will append the current platform to the axis name. So GetAxis("Horizontal")
will map to the HorizontalWINDOWS
axis in your input configuration within unity. Unfortunately you still have to map the axis, but it's only Horizontal and Vertical for each platform, so it's not too bad.
The real win here is being able to ask which button BY NAME is being pressed on the gamepad and get a platform specific answer, e.g.
// maps to KeyCode.JoystickButton8 and KeyCode.JoystickButton11 on windows and osx respectively
GetKeyDown(GamePadAbstraction.LEFT_THUMBSTICK);
To instantiate the input manager, just create a MonoBehaviour that sticks around (i've included a sample of my GameManager.cs
file here) and add a public field IInputSystem Input
then instantiate it on Awake()
with ```
private IInputSystem CreateInputSystem()
{
switch (Application.platform)
{
case RuntimePlatform.OSXEditor:
case RuntimePlatform.OSXPlayer:
return new OsxInputSystem();
default:
case RuntimePlatform.WindowsEditor:
case RuntimePlatform.WindowsPlayer:
return new WindowsInputSystem();
}
}```