CONTACT ME
If I don’t answer your message in 1-2 days I probably didn’t get a notification about new replies so email me at daemon3000@hotmail.com
Introduction
InputManager is a custom input manager that allows you to rebind keys at runtime and abstract input devices(through input configurations) for cross platform input.
Features
- Very simple to implement. It has the same public methods and variables as Unity’s Input class.
- Allows you to customize key bindings at runtime.
- Allows you to use XInput for better controller support.
- Allows you to convert touch input to axes and buttons on mobile devices.
- Allows you to bind script methods to various input events(e.g. when the user presses a button or key) through the inspector.
- Run up to four input configurations at the same time for easy local co-op input handling.
- Save the key bindings to a file, to PlayerPrefs or anywhere else by implementing a simple interface.
- Seamless transition from keyboard to gamepad with multiple bindings per input action.
- Standardized gamepad input. Gamepad profiles map various controllers to a standard set of buttons and axes.
Platforms
Works on Windows 7, Windows 8 Desktop, Windows 8 Store, Linux, Mac OSX, WebPlayer and Android(not tested on iOS but it might work).
Download
Visit the github page to get InputManager for free and read detailed instructions.
Getting Started
For detailed information on how to get started with this plugin visit the Wiki or watch the video tutorial linked below.
Code Samples
Key Remap
// Example 1: Changing axis properties
AxisConfiguration axisConfig = InputManager.GetAxisConfiguration("MoveVertical");
axisConfig.positive = KeyCode.W;
axisConfig.negative = KeyCode.S;
axisConfig.sensitivity = 2.0f;
axisConfig.invert = true;
// The AxisConfiguration class allows you to change any parameter that you can change in the custom editor window
// Example 2: Key remap
private void OnGUI()
{
// Scan for key
if(GUILayout.Button("Jump"))
{
// InputManager.StartKeyScan(KeyScanHandler scanHandler, float timeout, string cancelScanButton, params object[] optional)
InputManager.StartKeyScan((key, arg) => {
AxisConfiguration axisConfig = InputManager.GetAxisConfiguration("Jump");
axisConfig.positive = (key == KeyCode.Backspace) ? KeyCode.None : key;
return true;
}, 10.0f, null);
}
// Scan for joystick axis
if(GUILayout.Button("Move"))
{
// InputManager.StartJoystickAxisScan(AxisScanHandler scanHandler, int joystick, float timeout, string cancelScanButton, params object[] optional)
InputManager.StartJoystickAxisScan((axis, arg) => {
if(axis >= 0)
{
AxisConfiguration axisConfig = InputManager.GetAxisConfiguration("Move");
axisConfig.SetAnalogAxis(0, axis); // Use axisConfig.SetMouseAxis() to change a mouse axis
return true;
}
return false;
}, 0, 10.0f, null);
}
}
Save/Load
// Example 1: Default save and load
InputManager.Save(); // Saves the configurations in Application.persistentDataPath
InputManager.Load(); // Loads the configurations from Application.pesistentDataPath
InputManager.Save("MyGame/Profile/input_config.xml"); // Saves the configurations at a custom path
InputManager.Load("MyGame/Profile/input_config.xml"); // Loads the configurations from a custom path
// Example 2: Saving and loading using PlayerPrefs
private void PlayerPrefsSave()
{
StringBuilder output = new StringBuilder();
InputSaverXML saver = new InputSaverXML(output);
InputManager.Save(saver);
PlayerPerfs.SetString("MyGame.InputConfig", output.ToString());
}
private void PlayerPrefsLoad()
{
if(PlayerPrefs.HasKey("MyGame.InputConfig"))
{
string xml = PlayerPrefs.GetString("MyGame.InputConfig");
using(TextReader reader = new StringReader(xml))
{
InputLoaderXML loader = new InputLoaderXML(reader);
InputManager.Load(loader);
}
}
}
// Alternatively you can create a custom input saver and loader by implementing IInputSaver and IInputLoader
Create input at runtime
private void CreateKeyboardConfiguration()
{
InputManager.CreateInputConfiguration("Keyboard");
// public static AxisConfiguration CreateDigitalAxis(string configuration, string name, KeyCode positive,
// KeyCode negative, float gravity, float sensitivity)
InputManager.CreateDigitalAxis("Keyboard", "Horizontal", KeyCode.D, KeyCode.A, 3.0f, 3.0f);
InputManager.CreateDigitalAxis("Keyboard", "Vertical", KeyCode.W, KeyCode.S, 3.0f, 3.0f);
// public static AxisConfiguration CreateButton(string configuration, string name, KeyCode key)
InputManager.CreateButton("Keyboard", "Jump", KeyCode.Space);
InputManager.SetConfiguration("Keyboard");
}
private void CreateJoystickConfiguration()
{
InputManager.CreateInputConfiguration("Joystick");
// public static AxisConfiguration CreateAnalogAxis(string configuration, string name, int joystick, int axis,
// float sensitivity, float deadZone)
InputManager.CreateAnalogAxis("Joystick", "Horizontal", 0, 0, 1.0f, 0.1f);
InputManager.CreateAnalogAxis("Joystick", "Vertical", 0, 1, 1.0f, 0.1f);
InputManager.CreateButton("Joystick", "Jump", KeyCode.JoystickButton0);
InputManager.SetConfiguration("Joystick");
}
// Note: "CreateButton" and "CreateDigitalAxis" have overloads that take secondary keys
License
This software is released under the MIT license.