Unity supports a standardized Game Controller Input API provided by Apple as part of the standard Unity Input API.
Calling Input.GetJoystickNames
will enumerate the names of all attached controllers. Names follow the pattern: [$profile_type,$connection_type] joystick $number by $model
. $profile_type might be either “basic” or “extended”, $connection_type is either “wired” or “wireless”. It could be used to detect the kind of connected controller. It is worth re-checking this list every few seconds to detect if controllers have been attached or detached. Here’s a C# example:
private bool connected = false;
IEnumerator CheckForControllers() {
while (true) {
var controllers = Input.GetJoystickNames();
if (!connected && controllers.Length > 0) {
connected = true;
Debug.Log("Connected");
} else if (connected && controllers.Length == 0) {
connected = false;
Debug.Log("Disconnected");
}
yield return new WaitForSeconds(1f);
}
}
void Awake() {
StartCoroutine(CheckForControllers());
}
Cuando los controladores son detectados usted puede ocultar los controles táctiles en pantalla o modificarlos para complementar el input del controlador. La nueva tarea es revisar por el input del Controlador del Juego.
The actual input scheme is highly dependent on the type of game you are developing. But it’s essentially about reading axes and button states. Take the following 2D game stage as a simple example:
The player controls the bean character, which can move right or left, jump and fire at the bad guys. By default, the Unity Input “Horizontal” axis is mapped to basic profile game controller D-pad and the left analog stick on extended profile controllers. So the code used to move the character back and forth is pretty simple:
float h = Input.GetAxis("Horizontal");
if (h * rigidbody2D.velocity.x < maxSpeed)
rigidbody2D.AddForce(Vector2.right * h * moveForce);
You can set up jump and fire actions in Unity’s Input settings.
Access it from the Unity editor menu as follows: Edit > Project Settings, then select the Input category.
Seleccione el botón joystick “A” para la acción de “Saltar” y “X” para “Disparar”.
Open the following actions in the Unity Input settings and specify “joystick button 14” for “Jump” and “joystick button 15” for “Fire”.
El manejo de código luego se ve algo así:
if (Input.GetButtonDown("Jump") && grounded) {
rigidbody2D.AddForce(new Vector2(0f, jumpForce));
}
if (Input.GetButtonDown("Fire")) {
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(speed, 0);
}
The following cheatsheet should help you map controller input in the Unity Input settings:
Nombre | KeyCode | Eje |
---|---|---|
A | botón del joystick 14 | Eje del joystick 14 |
B | botón del joystick 13 | Eje del joystick 13 |
X | botón del joystick 15 | eje del joystick 15 |
Y | botón del joystick 12 | eje del joystick 12 |
El stick izquierdo | N/A | Eje 1 (X) - Horizontal, Eje 2 (Y) - Vertical |
El Stick Derecho | N/A | Eje 3 - Horizontal, Eje 4 - Vertical |
D-pad Up | joystick button 4 | Perfil básico solamente: Eje 1 (X) - Horizontal, Eje 2 (Y) - Vertical |
D-pad Right | joystick button 5 | Perfil básico solamente: Eje 1 (X) - Horizontal, Eje 2 (Y) - Vertical |
D-pad Down | joystick button 6 | Perfil básico solamente: Eje 1 (X) - Horizontal, Eje 2 (Y) - Vertical |
D-pad Left | joystick button 7 | Perfil básico solamente: Eje 1 (X) - Horizontal, Eje 2 (Y) - Vertical |
Pausar | botón del joystick 0 | N/A |
L1/R1 | botón del joystick 8/botón del joystick 9 | botón del joystick 8/botón del joystick 9 |
L2/R2 | botón del joystick 10/botón del joystick 11 | botón del joystick 10/botón del joystick 11 |
Unity only includes the Game Controller framework in the project if a script in the project references Input.GetJoystickNames
. Unity iOS Runtime loads the framework dynamically, if it is available.
La documentación de Apple explícitamente enuncia que el input del controlador debe ser opcional y su juego debería reproducirse sin ellos.