Rewired

m

Controller

Hardware

Getting a Controller

(To get a specific controller)

ReInput.controllers.GetController

player.controllers.GetController

player.controllers.GetControllerWithTag

(To get the mouse from)

(To get the keyboard from)

(Loop through all controllers)

(Loop through controllers assigned to a Player)

Receiving joystick connect and disconnect events

r

using UnityEngine;using Rewired;public MyClass : MonoBehaviour { void Awake() { ReInput.ControllerConnectedEvent += OnControllerConnected; ReInput.ControllerDisconnectedEvent += OnControllerDisconnected; ReInput.ControllerPreDisconnectEvent += OnControllerPreDisconnect; } void OnControllerConnected(ControllerStatusChangedEventArgs args) { // This function will be called when a controller is connected // You can get information about the controller that was connected via the args parameter Debug.Log("A controller was connected! Name = " + args.name + " Id = " + args.controllerId + " Type = " + controllerType); } void OnControllerDisconnected(ControllerStatusChangedEventArgs args) { // This function will be called when a controller is fully disconnected // You can get information about the controller that was disconnected via the args parameter Debug.Log("A controller was disconnected! Name = " + args.name + " Id = " + args.controllerId + " Type = " + controllerType); } void OnControllerPreDisconnect(ControllerStatusChangedEventArgs args) { // This function will be called when a controller is about to be disconnected // You can get information about the controller that is being disconnected via the args parameter // You can use this event to save the controller's maps before it's disconnected Debug.Log("A controller is being disconnected! Name = " + args.name + " Id = " + args.controllerId + " Type = " + controllerType); }}

ReInput.ControllerConnectedEvent

Receive an event when a controller is connected, register for

ReInput.ControllerPreDisconnectEvent

Receive an event when a controller is about to be disconnected, register for

ReInput.ControllerDisconnectedEvent

Receive an event when a controller is fully disconnected, register for

Assigning joysticks to players

r

Joysticks will be assigned to Players automatically if you have enabled and configured joystick auto-assignment in the Rewired Editor under Settings. However, if you wish to manually assign or unassign joysticks, you can do so with the following methods:

Player class

ReInput class

Software

Enabling and disabling controller maps

r

using Rewired;public class MyClass { void SetEnabledStateOnMapsInCategory(Player player, string categoryName, bool state) { // The quick way player.controllers.maps.SetMapsEnabled(state, categoryName); // The manual way foreach(ControllerMap map in player.controllers.maps.GetAllMapsInCategory(categoryName)) { map.enabled = state; // set the enabled state on the map } }}

//Controller Maps can be enabled or disabled at will via scripting. This can be useful if you want to change game modes and have a different set of controls become active. For example, opening a menu screen. Controller Maps are stored in the Player class.

player.controllers.maps.SetAllMapsEnabled

//Set enabled state on all maps owned by the Player or all maps for a specific controller type

player.controllers.maps.SetMapsEnabled

//Set enabled state on a group of maps owned by the Player by controller type, category, and layout

//You can also enable and disable maps one by one by accessing the maps through Playe

// Disabled maps will not contribute to input.

Once you have the map, enable or disable it by setting controllerMap.enabled = true or controllerMap.enabled = false

player.controllers.maps.GetMap

player.controllers.maps.GetMaps

player.controllers.maps.GetAllMaps

player.controllers.maps.GetAllMapsInCategory

(init)

Calibrating controller axes

Creating a controller mapping screen

Saving and loading maps

controller

r

private void SaveAllMaps() { // This example uses PlayerPrefs because its convenient, though not efficient, but you could use any data storage method you like. IList<Player> allPlayers = ReInput.players.AllPlayers; for(int i = 0; i < allPlayers.Count; i++) { Player player = allPlayers[i]; // Get all savable data from player PlayerSaveData playerData = player.GetSaveData(true); // Save Input Behaviors foreach(InputBehavior behavior in playerData.inputBehaviors) { string key = GetInputBehaviorPlayerPrefsKey(player, behavior); PlayerPrefs.SetString(key, behavior.ToXmlString()); // save the behavior to player prefs in XML format } // Save controller maps foreach(ControllerMapSaveData saveData in playerData.AllControllerMapSaveData) { string key = GetControllerMapPlayerPrefsKey(player, saveData); PlayerPrefs.SetString(key, saveData.map.ToXmlString()); // save the map to player prefs in XML format } } // Save joystick calibration maps foreach(Joystick joystick in ReInput.controllers.Joysticks) { JoystickCalibrationMapSaveData saveData = joystick.GetCalibrationMapSaveData(); string key = GetJoystickCalibrationMapPlayerPrefsKey(saveData); PlayerPrefs.SetString(key, saveData.map.ToXmlString()); // save the map to player prefs in XML format } // Save changes to PlayerPrefs PlayerPrefs.Save();}private void LoadAllMaps() { // This example uses PlayerPrefs because its convenient, though not efficient, but you could use any data storage method you like. IList<Player> allPlayers = ReInput.players.AllPlayers; for(int i = 0; i < allPlayers.Count; i++) { Player player = allPlayers[i]; // Load Input Behaviors - all players have an instance of each input behavior so it can be modified IList<InputBehavior> behaviors = ReInput.mapping.GetInputBehaviors(player.id); // get all behaviors from player for(int j = 0; j < behaviors.Count; j++) { string xml = GetInputBehaviorXml(player, behaviors[j].id); // try to the behavior for this id if(xml == null || xml == string.Empty) continue; // no data found for this behavior behaviors[j].ImportXmlString(xml); // import the data into the behavior } // Load the maps first and make sure we have them to load before clearing // Load Keyboard Maps List<string> keyboardMaps = GetAllControllerMapsXml(player, true, ControllerType.Keyboard, ReInput.controllers.Keyboard); // Load Mouse Maps List<string> mouseMaps = GetAllControllerMapsXml(player, true, ControllerType.Mouse, ReInput.controllers.Mouse); // load mouse controller maps // Load Joystick Maps bool foundJoystickMaps = false; List<List<string>> joystickMaps = new List<List<string>>(); foreach(Joystick joystick in player.controllers.Joysticks) { List<string> maps = GetAllControllerMapsXml(player, true, ControllerType.Joystick, joystick); joystickMaps.Add(maps); if(maps.Count > 0) foundJoystickMaps = true; } // Now add the maps to the controller // Keyboard maps if(keyboardMaps.Count > 0) player.controllers.maps.ClearMaps(ControllerType.Keyboard, true); // clear only user-assignable maps, but only if we found something to load. Don't _really_ have to clear the maps as adding ones in the same cat/layout will just replace, but let's clear anyway. player.controllers.maps.AddMapsFromXml(ControllerType.Keyboard, 0, keyboardMaps); // add the maps to the player // Joystick maps if(foundJoystickMaps) player.controllers.maps.ClearMaps(ControllerType.Joystick, true); // clear only user-assignable maps, but only if we found something to load. Don't _really_ have to clear the maps as adding ones in the same cat/layout will just replace, but let's clear anyway. int count = 0; foreach(Joystick joystick in player.controllers.Joysticks) { player.controllers.maps.AddMapsFromXml(ControllerType.Joystick, joystick.id, joystickMaps[count]); // add joystick controller maps to player count++; } // Mouse Maps if(mouseMaps.Count > 0) player.controllers.maps.ClearMaps(ControllerType.Mouse, true); // clear only user-assignable maps, but only if we found something to load. Don't _really_ have to clear the maps as adding ones in the same cat/layout will just replace, but let's clear anyway. player.controllers.maps.AddMapsFromXml(ControllerType.Mouse, 0, mouseMaps); // add the maps to the player } // Load joystick calibration maps foreach(Joystick joystick in ReInput.controllers.Joysticks) { joystick.ImportCalibrationMapFromXmlString(GetJoystickCalibrationMapXml(joystick)); // load joystick calibration map if any }}

calibration

r

private void SaveAllMaps() { // ... Removed controller map saving code // Save joystick calibration maps foreach(Joystick joystick in ReInput.controllers.Joysticks) { JoystickCalibrationMapSaveData saveData = joystick.GetCalibrationMapSaveData(); string key = GetJoystickCalibrationMapPlayerPrefsKey(saveData); PlayerPrefs.SetString(key, saveData.map.ToXmlString()); // save the map to player prefs in XML format } // Save changes to PlayerPrefs PlayerPrefs.Save();}private void LoadAllMaps() { // ... Removed controller map loading code // Load joystick calibration maps foreach(Joystick joystick in ReInput.controllers.Joysticks) { joystick.ImportCalibrationMapFromXmlString(GetJoystickCalibrationMapXml(joystick)); // load joystick calibration map if any }}

(runtime)

Modifying Input Behaviors during runtime

Creating on-screen touch controllers

(advanced)

Creating new controller definitions

Creating new controller templates

Adding a controller to an existing controller template

Identifying joysticks on fallback platforms

At game start, pop up a GUI asking the user to press a button on each named controller.

When the user presses the button, set the joystick id on the joystick so Rewired can identify which joystick it is.

Repeat the above steps for each joystick in the system.

Any time a joystick is connected or disconnected, repeat the whole identification process for every joystick.

Input Manager

Rewired Editor

Settings

//The Settings page allows you change the options for the current Input Manager.

Update Loop

Input Sources

Joystick Auto-Assignment

Players

Players

Actions

Input Behaviors

Map Categories

Action Categories

Custom Controllers

Joystick Layouts

Keyboard Layouts

Mouse Layouts

Custom Controller Layouts

Joystick Maps

Keyboard Maps

Mouse Maps

Custom Controller Maps

Getting a Player

ReInput.players.GetPlayer

r

using UnityEngine;using Rewired;public class MyClass : MonoBehaviour { public int playerId; private Player player; void Awake() { player = ReInput.players.GetPlayer(playerId); }}

Input

Actions

r

private Rewired.Player player;void Awake() { player = Rewired.ReInput.players.GetPlayer(0); // get the player by id}void Update() { player.GetButtonDown("Fire");}

//An Action can be any type of event that takes place as a result of input

r

Actions are generally how you get input in Rewired. Instead of getting input from a specific button on a specific controller which may change if the user switches controllers, you get input from the Action through the Player class

//You can also get input based on the Action Id instead of by name if you choose

//The Action Id is displayed in the Rewired Editor under Actions.

ex:

(char)

(sys)

(Input Behaviour)

(Getting Input)

Polling

r

using UnityEngine;using Rewired;public class MyClass : MonoBehaviour { public int playerId; private Player player; void Awake() { player = ReInput.players.GetPlayer(playerId); } void Update() { if(player.GetAxis("Move Horizontal") != 0.0f) { Debug.Log("Move Horizontal!"); } if(player.GetButtonDown("Fire")) { Debug.Log("Fire!"); } }}

Event

r

public class MyClass : MonoBehaviour { public int playerId; private Player player; void Awake() { player = ReInput.players.GetPlayer(playerId); // Add delegates to receive input events from the Player // This event will be called every frame any input is updated player.AddInputEventDelegate(OnInputUpdate, UpdateLoopType.FixedUpdate); // This event will be called every frame the "Fire" action is updated player.AddInputEventDelegate(OnFireUpdate, UpdateLoopType.FixedUpdate); // This event will be called when the "Fire" button is first pressed player.AddInputEventDelegate(OnFireButtonDown, UpdateLoopType.FixedUpdate, InputActionEventType.ButtonJustPressed); // This event will be called when the "Fire" button is first released player.AddInputEventDelegate(OnFireButtonUp, UpdateLoopType.FixedUpdate, InputActionEventType.ButtonJustReleased); // The update loop you choose for the event matters. Make sure your chosen update loop is enabled in // the Settings page of the Rewired editor or you won't receive any events. } void OnInputUpdate(InputActionEventData data) { switch(data.actionName) { // determine which action this is case "Move Horizontal": if(data.GetAxis() != 0.0f) Debug.Log("Move Horizontal!"); break; case "Fire": if(data.GetButtonDown()) Debug.Log("Fire!"); break; } } void OnFireUpdate(InputActionEventData data) { if(data.GetButtonDown()) Debug.Log("Fire!"); } void OnFireButtonDown(InputActionEventData data) { Debug.Log("Fire!"); } void OnFireButtonUp(InputActionEventData data) { Debug.Log("Fire Released!"); }}

Controller

//A third method would be to get input directly from a controller's buttons and axes. This is generally not a good choice as you will lose all the advantages of the player-centric system including mapping.