Unity 支持键盘、游戏杆和游戏手柄输入。
可以在 Input 窗口中创建虚拟轴和按钮,而终端用户可以在友好的屏幕配置对话框中配置键盘输入。
可以设置游戏杆、游戏手柄、键盘和鼠标,然后通过一个简单的脚本接口访问它们。通常会使用轴和按钮来模仿游戏主机控制器。也可以访问键盘上的按键。
从脚本中,可以通过名称访问所有虚拟轴。
每个项目在创建时都具有以下默认输入轴:
如果要添加新的虚拟轴,请选择 Edit > Project Settings 菜单,然后选择 Input 类别。还可以在此处更改每个轴的设置。
可以将每个轴映射到游戏杆上的两个按钮、鼠标或键盘键。
属性: | 功能: |
---|---|
Name | 用于从脚本中检查此轴的字符串名称。 |
Descriptive Name | 独立构建的 Configuration 对话框的 Input 选项卡中显示的正值名称。 |
Descriptive Negative Name | 独立构建的 Configuration 对话框的 Input 选项卡中显示的负值名称。 |
Negative Button | 用于向负方向推动轴的按钮。 |
Positive Button | 用于向正方向推动轴的按钮。 |
Alt Negative Button | 用于向负方向推动轴的替代按钮。 |
Alt Positive Button | 用于向正方向推动轴的替代按钮。 |
Gravity | 未按下按钮的情况下,轴下降到中性点的速度(以单位/秒表示)。 |
Dead | 模拟盲区的大小。此范围内的所有模拟设备值都会映射到中性点。 |
Sensitivity | 轴向目标值移动的速度(以单位/秒表示)。仅用于数字设备。 |
Snap | 如果启用,按下相反方向的按钮时,轴值将重置为零。 |
Invert | 如果启用,则负按钮 (Negative Buttons) 将提供正值,反之亦然。 |
Type | 将控制此轴的输入类型。 |
轴 | 将控制此轴的已连接设备的轴。 |
Joy Num | 将控制此轴的已连接游戏杆。 |
使用这些设置可以微调输入的外观。所有这些在 Editor 中的工具提示中也有说明。
可以从脚本中查询当前状态,如下所示:
value = Input.GetAxis ("Horizontal");
轴的值介于 –1 到 1 之间。中性位置为 0。 这是游戏杆输入和键盘输入的情况。
但是,Mouse Delta 和 Window Shake Delta 是鼠标或窗口在最后一帧中移动的程度。这意味着,当用户快速移动鼠标时,它可以大于 1 或小于 –1。
可以使用相同的名称创建多个轴。获取输入轴时,将返回绝对值最大的轴。这样就可以将多个输入设备分配给一个轴名称。例如,为键盘输入创建一个轴,用相同名称为游戏杆输入创建一个轴。如果用户正在使用游戏杆,则输入将来自游戏杆,否则输入将来自键盘。这样一来,不必在编写脚本时考虑输入的来源。
要将键映射到轴,必须在 Inspector 中的 Positive Button 或 Negative Button 属性中输入键的名称。
键的名称遵循以下约定:
用于标识键的名称在脚本接口中和在 Inspector 中是相同的。
value = Input.GetKey ("a");
另外请注意,可以使用 KeyCode 枚举参数来访问键。
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.