Input

class in UnityEngine

Switch to Manual

Description

访问输入系统的接口。

Use this class to read the axes set up in the Conventional Game Input, 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. See Input Manager for this.

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. Do not use it for movement. Input.GetAxis will make the script code more small and simple.

Note also that the Input flags are not reset until Update. It is suggested you make all the Input calls in the Update Loop.

See Also: KeyCode which lists all of the key press, mouse and joystick options.

移动设备:

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.

设备移动时,其加速度计硬件将报告设备沿三维空间中的三个主轴的线性加速度变化。 您可以使用该数据检测设备的当前方向(相对于地面)以及该方向上的任何即时更改。

硬件将沿每个轴的加速度直接报告为重力值。 值 1.0 表示沿给定轴约 +1g 的荷载,值 -1.0 表示 -1g。 当将设备竖直握住(主按钮位于底部)举到您正前方时, 右侧为正 X 轴,上方为正 Y 轴,指向您的方向为正 Z 轴。

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.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public void Update() { if (Input.GetButtonDown("Fire1")) { Debug.Log(Input.mousePosition); } } }

This component relates to legacy methods for drawing UI textures and images to the screen. You should instead use UI system. This is also unrelated to the IMGUI system.

Static Variables

acceleration最近测量的设备在三维空间中的线性加速度。(只读)
accelerationEventCount上一帧期间进行的加速度测量的数量。
accelerationEvents返回上一帧期间进行的加速度测量的列表。(只读)(分配临时变量)。
anyKey当前是否有任何键或鼠标按钮处于按下状态?(只读)
anyKeyDown在用户按任意键或鼠标按钮后的第一帧返回 true。(只读)
backButtonLeavesApp Back 按钮是否应该退出应用程序?仅适用于 Android、Windows Phone 或 Windows 平板电脑。
compass用于访问罗盘的属性(仅限手持设备)。(只读)
compensateSensors该属性控制是否应该对输入传感器补偿屏幕方向。
compositionCursorPosIME 用于打开窗口的当前文本输入位置。
compositionString用户键入的当前 IME 组合字符串。
deviceOrientation操作系统报告的设备的物理方向。(只读)
gyro返回默认陀螺仪。
imeCompositionMode控制 IME 输入组合的启用和禁用。
imeIsSelected用户是否选择了 IME 键盘输入源?
inputString返回该帧输入的键盘输入。(只读)
location用于访问设备位置的属性(仅限手持设备)。(只读)
mousePosition鼠标当前的像素坐标位置。(只读)
mousePresent指示是否检测到鼠标设备。
mouseScrollDelta当前的鼠标滚动增量。(只读)
multiTouchEnabled该属性指示系统是否处理多点触控。
simulateMouseWithTouches启用/禁用通过触摸模拟鼠标操作。默认情况下,该选项已启用。
stylusTouchSupported当设备或平台支持 Stylus Touch 时,返回 true。
touchCount触摸次数。保证在整个帧期间不会更改。(只读)
touches返回表示上一帧中所有触摸状态的对象列表。(只读)(分配临时变量)。
touchPressureSupported供用户检查是否支持触摸压力的 bool 值。
touchSupported返回当前正在运行应用程序的设备是否支持触摸输入。

Static Functions

GetAccelerationEvent返回上一帧期间进行的特定加速度测量。(不分配临时变量)。
GetAxis返回由 axisName 标识的虚拟轴的值。
GetAxisRaw返回由 axisName 标识的虚拟轴的值(未应用平滑过滤)。
GetButton当按住 buttonName 标识的虚拟按钮时,返回 true。
GetButtonDown在用户按下由 buttonName 标识的虚拟按钮的帧期间返回 true。
GetButtonUp在用户释放由 buttonName 标识的虚拟按钮的第一帧返回 true。
GetJoystickNames返回描述连接的游戏杆的字符串数组。
GetKey在用户按下 name 标识的键时返回 true。
GetKeyDown在用户开始按下 name 标识的键的帧期间返回 true。
GetKeyUp在用户释放 name 标识的键的帧期间返回 true。
GetMouseButton返回是否按下了给定的鼠标按钮。
GetMouseButtonDown在用户按下给定鼠标按钮的帧期间返回 true。
GetMouseButtonUp在用户释放给定鼠标按钮的帧期间返回 true。
GetTouchCall Input.GetTouch to obtain a Touch struct.
IsJoystickPreconfigured确定 Unity 是否已预先配置某个特定的游戏杆模型。(仅限 Linux)。
ResetInputAxes重置所有输入。调用 ResetInputAxes 后,所有轴和所有按钮都恢复为 0,并且持续一帧时长。