Categorías: Todo - input - settings - sources - players

por Mike Ton hace 9 años

1325

Unity3D_Rewired

The document outlines various features and settings related to input management and joystick configuration within a system. It details options for joystick auto-assignment, including limits on the number of joysticks per player and strategies for distributing joysticks evenly among players.

Unity3D_Rewired

Rewired

Input

(Getting Input)

//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.

Event

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!");

}

}

Polling

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!");

}

}

}

(Input Behaviour)

private Rewired.Player player;

void Awake() {

player = Rewired.ReInput.players.GetPlayer(0); // get the player by id

}

void Update() {

player.GetButtonDown("Fire");

}

ex:

(sys)

Menu

Cancel

Load

Save

(char)

(attack)

Melee

Kick

Punch

Fire

Change Weapon

Reload

(vstate)

Crouch

Jump

(move)

Vertical

Horizontal

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

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

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

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

Getting a Player

ReInput.players.GetPlayer

using UnityEngine;

using Rewired;

public class MyClass : MonoBehaviour {

public int playerId;

private Player player;

void Awake() {

player = ReInput.players.GetPlayer(playerId);

}

}

Input Manager

Rewired Editor
Custom Controller Maps
Mouse Maps
Keyboard Maps
Joystick Maps
Custom Controller Layouts
Mouse Layouts
Keyboard Layouts
Joystick Layouts
Custom Controllers
Action Categories
Map Categories
Input Behaviors
Actions
Settings

Players

Joystick Auto-Assignment

Reassign To Previous Owner

When a joystick is disconnected and reconnected, attempt to reassign the joystick to the last player it was assigned to.

Distribute Joysticks Evenly

When auto-assigning joysticks, assign to players with the fewest number of joysticks first. Will assign up to the limit of Max Joysticks Per Player.

Assign To Playing Players Only

Only assign joysticks to players that have isPlaying = true. Non-playing players will not be assigned joysticks.

Max Joysticks Per Player

How many joysticks is a player allowed to be assigned? If enabled, connected joysticks will be assigned to each player up to this limit.

Auto-Assign Joysticks

Automatically assign joysticks to players as they are connected.

Input Sources

Always use Unity Input

Force Unity Input to be the input source. This will disable all external input sources such as Raw Input, Direct Input, XInput, and native OSX.

WARNING: Unity Input does not allow hot-plugging on most platforms. If you need hot-plugging, do not check this box.

Native Mouse Handling

Use native libraries to handle the mouse if available on the current platform. Enabling this will eliminate certain bugs associated with Unity's mouse handling.

NOTE: This setting currently only affects the Windows Standalone platform.

Use XInput

On Windows, use XInput for compatible devices. Otherwise fall back to Raw Input or Direct Input. Installation of DirectX end-user runtimes may be required for XInput support.

Windows Standalone

Direct Input

Use Direct Input for the input source. Installation of DirectX end-user runtimes may be required for Direct Input support.

Raw Input

Use Raw Input for the input source.

Update Loop

OnGUI

Updates input only in the OnGUI loop. Only necessary if you need GetButtonDown to work within the Unity GUI.

FixedUpdate

Updates input only in the FixedUpdate loop

If you try to get input from the Update loop, you will miss some input data

??? DOES THIS MEAN NOT TO USE IN COMBINATION WITH UPDATE LOOP?

Update

Updates input only in the Update loop

Ex: GetButtonDown and GetKeyDown will not always return correctly.

If you try to get input from the FixedUpdate loop, you will miss some input data

??? DOES THIS MEAN NOT TO USE IN COMBINATION WITH FixedUpdate LOOP?

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

Controller

Software
(advanced)

Identifying joysticks on fallback platforms

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

Repeat the above steps for each joystick in the system.

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

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

Adding a controller to an existing controller template

Creating new controller templates

Creating new controller definitions

(runtime)

Creating on-screen touch controllers

Modifying Input Behaviors during runtime

Saving and loading maps

calibration

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

}

}

controller

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

}

}

(init)

Creating a controller mapping screen

Calibrating controller axes

Enabling and disabling controller maps

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

}

}

}

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

player.controllers.maps.GetAllMapsInCategory

player.controllers.maps.GetAllMaps

player.controllers.maps.GetMaps

player.controllers.maps.GetMap

// 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.SetMapsEnabled

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

player.controllers.maps.SetAllMapsEnabled

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

//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.

Hardware
Assigning joysticks to players

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:

ReInput class

ReInput.controllers.RemoveControllerFromAllPlayers

ReInput.controllers.IsControllerAssignedToPlayer

ReInput.controllers.IsControllerAssigned

Player class

player.controllers.ClearAllControllers

player.controllers.ClearControllersOfType

player.controllers.ContainsController

player.controllers.RemoveController

player.controllers.AddController

Receiving joystick connect and disconnect events

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.ControllerDisconnectedEvent

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

ReInput.ControllerPreDisconnectEvent

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

ReInput.ControllerConnectedEvent

Receive an event when a controller is connected, register for

Getting a Controller

(Loop through controllers assigned to a Player)

player.controllers.CustomControllers

player.controllers.Joysticks

(Loop through all controllers)

ReInput.controllers.CustomControllers

ReInput.controllers.Joysticks

ReInput.controllers.Controllers

(To get the keyboard from)

player.controllers.Keyboard

ReInput.controllers.Keyboard

(To get the mouse from)

player.controllers.Mouse

ReInput.controllers.Mouse

(To get a specific controller)

player.controllers.GetControllerWithTag

player.controllers.GetController

ReInput.controllers.GetController