- Home /
Platform Dependent Project Settings.
In ProjectSettings>Input, I can conveniently set a mapping from joystick axes to functions in my game.
Unfortunately, axis numbers differ on platforms. If we take, e.g. the XBox 360 controller, it will have the L/R triggers on axes 9+10 on Windows. Yet, it is 3+6 on Linux, and 5+6 on Mac.
The documentation recommends platform specific mappings. But how can I achieve this?
Is there a way to have multiple versions of ProjectSettings/InputManager.asset and have unity pick the correct one, based on which platform it is running (both in-editor and stand alone?)
Answer by guavaman · Jan 22, 2019 at 08:01 PM
You can't do that, at least not officially. You could swap out the InputManager.asset file every time you make a build, but it wouldn't help if you want to support more than one gamepad type.
The normal way of handling cross-platform input is by either creating your own input manager or buying an input manager system from the Asset Store. Most input managers that wrap UnityEngine.Input will create an exhaustive list of Axes in the input manager, one for each possible axis on every joystick id. You poll the Axes in the input manager for all connected joysticks to get the axis values. For buttons, you poll Input.GetKey using KeyCodes for all buttons on all possible joysticks. Then, to make things cross-platform, you need a system that maps from a pre-defined controller definition that you've created (for each supported platform) to a standardized layout. Then your code can just reference the standardized layout. It can get very involved and be very time consuming to create an input manager like this, especially if you want to support more than a few controllers. There are some good input managers on the Asset Store that can do this including my own Rewired, which does this and a whole lot more.
Adding to this, Unity's got their new Input system up as a Preview package, which should help relieve some of the cross-platform headaches.
Until then (assu$$anonymous$$g you don't want to build with preview packages), you will probably want to define those cross-platform values yourself. This was an example input manager I made for a project awhile ago which tackles the same problem:
https://pastebin.com/AxdkVwTr
The part you probably want is towards the bottom, a static class called ControlCodes that defines xbox input across platforms.
Your answer
Follow this Question
Related Questions
When will Unity officially support Windows 8 and Windows Phone 8 platforms? 1 Answer
Can I install tools for different platforms after installing the editor? 1 Answer
Disabling options on the Input Manager 1 Answer
Keep crouched until button pressed again? Javascript provided 1 Answer
trouble getting inputs from six axis in the joystick 1 Answer