Unity: default input on different platforms and my workaround

In Brazil we have a word that I don’t think we can inarguably decide the translation to english: gambiarra. It stands for a dirty workaround, something that works but may break at any moment and may be a pain to resolve. Well, a gambiarra is what I did to circumvent one of Unity’s input system problems.

After finishing RollFTW for the jam, I wanted to improve the input handling to support different platforms, other than desktops with keyboard, just for learning. I usually like to create a API that offers the buttons and axes from a XBOX gamepad, and then, depending on the platform, fetch these informations in specific ways, so I created a central static class to standardize that. However, when supporting the actual XBOX gamepad, my main problem arose:

In Unity’s Input Manager, you can’t set the default main button per platform.

Which is a bummer because the button mappings are pretty different between platforms (i.g. button A is joystick button 0 in Windows and joystick button 16 in OSX). I started doing something like this:

Code horror
Creepy!

I think you can imagine how these would be used. But, then, on a game that showed the default Unity configuration window, setting up the controls would be almost impossible for the player. I also don’t want to just leave the Windows configuration as default for all platforms, as I like to prevent the player from having to configure stuff as much as possible.

Enters my gambiarra: inside the project folder, I replicated the InputManager.asset file for each platform (Windows, Linux and OSX), and my build script would rename them as needed before compiling the game.

Input Manager replicated assets

You can see my full build script here (sorry, Windows users!):

#!/bin/bash

GAME_NAME=RollFTW
MAIN_PATH=~/Desktop
UNITY_PATH=/Applications/Unity/Unity.app/Contents/MacOS/Unity
FINAL_PATH=$MAIN_PATH/$GAME_NAME

CURRENT_PATH=$(pwd)

rm -rf $FINAL_PATH
mkdir $FINAL_PATH

cd $FINAL_PATH

mkdir web
mkdir linux
mkdir win32
mkdir osx

cd $CURRENT_PATH

$UNITY_PATH -quit -batchmode -buildWebPlayer $FINAL_PATH/web/$GAME_NAME
cp -f ./ProjectSettings/InputManager-linux.asset ./ProjectSettings/InputManager.asset
$UNITY_PATH -quit -batchmode -buildLinux32Player $FINAL_PATH/linux/$GAME_NAME.x86
cp -f ./ProjectSettings/InputManager-win32.asset ./ProjectSettings/InputManager.asset
$UNITY_PATH -quit -batchmode -buildWindowsPlayer $FINAL_PATH/win32/$GAME_NAME.exe
cp -f ./ProjectSettings/InputManager-osx.asset ./ProjectSettings/InputManager.asset
$UNITY_PATH -quit -batchmode -buildOSXPlayer $FINAL_PATH/osx/$GAME_NAME.app

cd $FINAL_PATH

cp -f web/$GAME_NAME/*.unity3d ./
rm -rf win32/*.pdb

cd $FINAL_PATH/linux
zip -r -X -q ../$GAME_NAME-linux *
cd $FINAL_PATH/win32
zip -r -X -q ../$GAME_NAME-win32 *
cd $FINAL_PATH/osx
zip -r -X -q ../$GAME_NAME-osx *

cd $FINAL_PATH

rm -rf web
rm -rf linux
rm -rf win32
rm -rf osx

A small note is that I needed to change the sensitivity of the axes to 3, not sure why. I guess this can be a OSX related issue.

Suggestions? Leave a comment below!

Leave a Reply

Your email address will not be published. Required fields are marked *