Input 输入

Interface into the Input system.


Use this class to read the axes set up in the Input Manager, and to access multi-touch/accelerometer data on mobile devices.


To read an axis use Input.GetAxis with one of the following default axes: "Horizontal" and "Vertical" are mapped to joystick, A, W, S, D and the arrow keys. "Mouse X" and "Mouse Y" are mapped to the mouse delta. "Fire1", "Fire2" "Fire3" are mapped to Ctrl, Alt, Cmd keys and three mouse or joystick buttons. New input axes can be added in the Input Manager.

想要读取轴向使用Input.GetAxis方法获取下列默认轴: "Horizontal" 和"Vertical" 映射于控制杆、A、W、S、D和箭头键(方向键)。 "Mouse X" 和"Mouse Y" 映射于鼠标,"Fire1", "Fire2" "Fire3"映射于键盘的Ctrl、Alt、Cmd键和鼠标中键或控制器的按钮。新的输入设置可以使用输入管理器来添加。

If you are using input for any kind of movement behaviour use Input.GetAxis. It gives you smoothed and configurable input that can be mapped to keyboard, joystick or mouse. Use Input.GetButton for action like events only. Don't use it for movement, Input.GetAxis will make the script code smaller and simpler.


Note also that the Input flags are not reset until "Update()", so its suggested you make all the Input Calls in the Update Loop.


Mobile Devices:


iOS and Android devices are capable of tracking multiple fingers touching the screen simultaneously. You can access data on the status of each finger touching screen during the last frame by accessing the Input.touches property array.


As a device moves, its accelerometer hardware reports linear acceleration changes along the three primary axes in three-dimensional space. You can use this data to detect both the current orientation of the device (relative to the ground) and any immediate changes to that orientation.

当设备移动时,它们的加速感应器硬件将报告它们在三维空间中沿着三个主轴的线性加速变化数据。你可以使用这些数据检测设备当前的移动方向(相对于地面)和突然间的方向改 变。

Acceleration along each axis is reported directly by the hardware as G-force values. A value of 1.0 represents a load of about +1g along a given axis while a value of -1.0 represents -1g. If you hold the device upright (with the home button at the bottom) in front of you, the X axis is positive along the right, the Y axis is positive directly up, and the Z axis is positive pointing toward you.


You can read the Input.acceleration property to get the accelerometer reading. You can also use the Input.deviceOrientation property to get a discrete evaluation of the device's orientation in three-dimensional space. Detecting a change in orientation can be useful if you want to create game behaviors when the user rotates the device to hold it differently.


Note that the accelerometer hardware can be polled more than once per frame. To access all accelerometer samples since the last frame, you can read the Input.accelerationEvents property array. This can be useful when reconstructing player motions, feeding acceleration data into a predictor, or implementing other precise motion analysis.


The Unity mobile devices input API is based on Apple's iOS API. It may help to learn more about the native API to better understand Unity's Input API. You can find Apple's input API documentation here:

Unity移动设备输入API是以苹果的iOS API为基础。学习一些原生API更有助于理解Unity的Input API。你可以查找苹果的Input API文档: