Unity 支持 Apple 在标准 Unity 输入 API 中提供的标准化游戏控制器输入 API。
调用 Input.GetJoystickNames
将枚举所有连接的控制器的名称。名称遵循以下模式:[$profile_type,$connection_type] joystick $number by $model
。$profile_type 可以是“basic”或“extended”,$connection_type 为“wired”或“wireless”。此名称可用于检测连接的控制器的类型。最好是每隔几秒重新检查一次该列表,以便检测是否已连接或断开控制器。下面是一个 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());
}
当检测到控制器时,可以隐藏屏幕上的触摸控件或修改它们以补充控制器输入。下一个任务是检查游戏控制器输入。
实际输入方案高度依赖于所开发的游戏类型。但主要涉及读取轴和按钮状态。以下面的 2D 游戏阶段作为一个简单的例子:
玩家控制豆子角色,使其可向左或向右移动、跳跃以及射击坏人。默认情况下,Unity 输入“Horizontal”轴映射到基本配置游戏控制器方向键和扩展配置控制器上的左模拟摇杆。所以用于来回移动角色的代码非常简单:
float h = Input.GetAxis("Horizontal");
if (h * rigidbody2D.velocity.x < maxSpeed)
rigidbody2D.AddForce(Vector2.right * h * moveForce);
可在 Unity 的 Input 设置中设置跳跃和射击操作。
在 Unity Editor 菜单中按如下方式访问该设置:__Edit__ > Project Settings__,然后选择 Input__ 类别。
为“Jump”操作选择游戏杆按钮“A”,并为“Fire”操作选择“X”。
在 Unity Input 设置中打开以下操作,然后为“Jump”指定“游戏杆按钮 14”,并为“Fire”指定“游戏杆按钮 15”。
然后,代码处理如下所示:
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);
}
以下备忘单应该可以帮助您对应 Unity Input 设置中的控制器输入:
名称 | KeyCode | 轴 |
---|---|---|
A | 游戏杆按钮 14 | 游戏杆轴 14 |
B | 游戏杆按钮 13 | 游戏杆轴 13 |
X | 游戏杆按钮 15 | 游戏杆轴 15 |
是 | 游戏杆按钮 12 | 游戏杆轴 12 |
左摇杆 | 无 | 轴 1 (X) - 水平,轴 2 (Y) - 垂直 |
右摇杆 | 无 | 轴 3 - 水平,轴 4 - 垂直 |
向上方向键 | 游戏杆按钮 4 | 仅限基本配置:轴 2 (Y) |
向右方向键 | 游戏杆按钮 5 | 仅限基本配置:轴 1 (X) |
向下方向键 | 游戏杆按钮 6 | 仅限基本配置:轴 2 (Y) |
向左方向键 | 游戏杆按钮 7 | 仅限基本配置:轴 1 (X) |
暂停 | 游戏杆按钮 0 | 无 |
L1/R1 | 游戏杆按钮 8/游戏杆按钮 9 | 游戏杆轴 8/游戏杆轴 9 |
L2/R2 | 游戏杆按钮 10/游戏杆按钮 11 | 游戏杆轴 10/游戏杆轴 11 |
如果项目中的脚本引用了 Input.GetJoystickNames
,Unity 只在项目中包含游戏控制器 (Game Controller) 框架。Unity iOS 运行时会动态加载该框架(如果可用)。
Apple 文档明确指出控制器输入必须为可选,游戏应该可以在没有控制器输入的情况下操作。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.