Version: 5.6
iOS 脚本
高级 Unity 移动端脚本

iOS 游戏控制器支持

Starting with OS 7, a standardized Game Controller Input API is provided by Apple. Unity support for this API comes 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 an example how of to do it in C#:

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());
}

当检测到控制器时,可以隐藏屏幕上的触摸控件或修改它们以补充控制器输入。下一个任务是检查游戏控制器输入。

处理输入

Actual input scheme is highly dependent on the type of game you are developing. But in any case it goes down to reading axes and button states. Take following 2D game stage as 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 dpad 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 Manager.

  • Access it from the Unity editor menu as follows: Edit > Project Settings > Input.

  • 为“Jump”操作选择游戏杆按钮“A”,并为“Fire”操作选择“X”。

  • Open the following actions in the Unity Input Manager and specify “joystick button 14” for “Jump” and “joystick button 15” for “Fire”.

然后,代码处理如下所示:

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 Manager:

名称 KeyCode
A 游戏杆按钮 14 游戏杆轴 14
B 游戏杆按钮 13 游戏杆轴 13
X 游戏杆按钮 15 游戏杆轴 15
游戏杆按钮 12 游戏杆轴 12
左摇杆 轴 1 (X) - 水平,轴 2 (Y) - 垂直
右摇杆 轴 3 - 水平,轴 4 - 垂直
Dpad Left/Right joystick button 7 / joystick button 5 仅限基本配置:轴 1 (X)
Dpad Up/Down joystick button 4 / joystick button 6 仅限基本配置:轴 2 (Y)
暂停 游戏杆按钮 0
L1/R1 游戏杆按钮 8/游戏杆按钮 9 游戏杆轴 8/游戏杆轴 9
L2/R2 游戏杆按钮 10/游戏杆按钮 11 游戏杆轴 10/游戏杆轴 11

有关游戏控制器 API 支持的最后说明

The Game Controller framework is loaded dynamically by Unity iOS runtime only if it is available. For older iOS versions it will just return an empty list of controllers.

Apple 文档明确指出控制器输入必须为可选,游戏应该可以在没有控制器输入的情况下操作。

iOS 脚本
高级 Unity 移动端脚本