Input 输入

class in UnityEngine

/

Implemented in:UnityEngine.InputLegacyModule

UnityEngine 中的类

/

实现于:UnityEngine.InputLegacyModule

Description 描述

Interface into the Input system.

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.

输入系统的接口。

使用此类可以读取 Conventional Game Input 中设置的轴, 并访问移动设备上的多点触摸/加速度计数据。

读取一个轴使用 Input.GetAxis 使用下列默认轴中的一个:“Horizontal”和“Vertical”被映射到操纵杆, A, W, S, D 和箭头键。“Mouse X”和“Mouse Y”映射到鼠标增量。“Fire1”,“Fire2”,“Fire3”被映射到 Ctrl, Alt, Cmd 键和三个鼠标或操纵杆的按钮。可以添加新的输入轴, 请参见 Input Manager

如果将输入用于任何类型的移动行为,请使用 Input.GetAxis. 。它为您提供了平滑且可配置的输入,可以将其映射到键盘,操纵杆或鼠标。使用 Input.GetButton 只能执行类似事件的操作。请勿将其用于运动。 Input.GetAxis 将使脚本代码更小,更简单。

还要注意,在 Update 之前不会重置 Input标记 。建议您在 Update 循环中进行所有 Input 调用 。

另请参见:KeyCode ,其中列出了所有按键,鼠标和​​操纵杆选项。




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.



Mobile Devices ( 移动设备 ) :

iOS和Android设备能够跟踪同时触摸屏幕的多个手指。您可以通过访问 Input.touches 属性数组来访问最后一帧期间每个手指触摸屏状态的数据。

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

硬件将沿每个轴的加速度直接报告为G力值。值1.0表示沿给定轴的大约+ 1g负载,而值-1.0表示-1g。如果将设备竖直摆放在前面(主页按钮位于底部),则X轴沿右侧为正,Y轴正上方为正,Z轴为正指向您。

您可以读取 Input.acceleration 属性以获取加速度计读数。您还可以使用 Input.deviceOrientation 属性来获取三维空间中设备方向的离散评估。如果要在用户旋转设备以不同的方式握持设备时创建游戏行为,则检测方向变化可能很有用。

请注意,加速度计硬件每帧可以被轮询多次。要访问自上一帧以来的所有加速度计样本,可以读取 Input.accelerationEvents 属性数组。这在重建玩家运动,将加速度数据输入到预测变量或实施其他精确运动分析时很有用。

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.

这个组件与用于将UI纹理和图像绘制到屏幕上的旧方法有关。您应该改用UI系统。这也与IMGUI系统无关。

Static Properties 静态属性

加速度 acceleration 上次测量的设备在三维空间中的线性加速度。(只读) Last measured linear acceleration of a device in three-dimensional space. (Read Only)
加速事件计数 accelerationEventCount 在最后一帧期间发生的加速度测量次数。 Number of acceleration measurements which occurred during last frame.
加速度事件(s) accelerationEvents 返回在最后一帧期间发生的加速度测量的列表。(只读)(分配临时变量)。 Returns list of acceleration measurements which occurred during the last frame. (Read Only) (Allocates temporary variables).
任何键 anyKey 当前是否按住任何键或鼠标按钮?(只读) Is any key or mouse button currently held down? (Read Only)
任何键按下 anyKeyDown 当用户按下任何键或鼠标按钮的第一帧时返回true。(只读) Returns true the first frame the user hits any key or mouse button. (Read Only)
后退按钮应用程序 backButtonLeavesApp 是否应使用“后退”按钮退出该应用程序?仅在Android,Windows Phone或Windows Tablets上可用。 Should Back button quit the application?Only usable on Android, Windows Phone or Windows Tablets.
指南针(罗盘) compass 访问指南针的属性(仅限手持设备)。(只读) Property for accessing compass (handheld devices only). (Read Only)
补偿传感器 compensateSensors 此属性控制是否应针对屏幕方向补偿输入传感器。 This property controls if input sensors should be compensated for screen orientation.
组成光标位置 compositionCursorPos IME用于打开窗口的当前文本输入位置。 The current text input position used by IMEs to open windows.
组成字符串 compositionString 用户正在输入当前的IME组成字符串。 The current IME composition string being typed by the user.
设备定位 deviceOrientation 操作系统报告的设备物理方向。(只读) Device physical orientation as reported by OS. (Read Only)
陀螺仪 gyro 返回默认陀螺仪。 Returns default gyroscope.
IME组合模式 imeCompositionMode 控制启用和禁用IME输入构成。 Controls enabling and disabling of IME input composition.
IME是否选中 imeIsSelected 用户是否选择了IME键盘输入源? Does the user have an IME keyboard input source selected?
输入字符串 inputString 返回在此帧中输入的键盘输入。(只读) Returns the keyboard input entered this frame. (Read Only)
(地理)位置 location 用于访问设备位置的属性(仅限手持设备)。(只读) Property for accessing device location (handheld devices only). (Read Only)
鼠标位置 mousePosition 以像素坐标表示的当前鼠标位置。(只读) The current mouse position in pixel coordinates. (Read Only)
鼠标现状 mousePresent 指示是否检测到鼠标设备。 Indicates if a mouse device is detected.
鼠标滚动增量 mouseScrollDelta 当前的鼠标滚动增量。(只读) The current mouse scroll delta. (Read Only)
多点触摸启用 multiTouchEnabled 指示系统是否处理多次触摸的属性。 Property indicating whether the system handles multiple touches.
用触摸来模拟鼠标 simulateMouseWithTouches 启用/禁用带有触摸的鼠标模拟。默认情况下启用此选项。 Enables/Disables mouse simulation with touches. By default this option is enabled.
笔触摸支持 stylusTouchSupported 当设备或平台支持Stylus Touch时,返回true。 Returns true when Stylus Touch is supported by a device or platform.
触摸数 touchCount 触摸次数。保证在整个框架中不变。(只读) Number of touches. Guaranteed not to change throughout the frame. (Read Only)
触摸列表 touches 返回代表上一帧中所有触摸状态的对象列表。(只读)(分配临时变量)。 Returns list of objects representing status of all touches during last frame. (Read Only) (Allocates temporary variables).
接触压力支持 touchPressureSupported 布尔值,可让用户检查是否支持触摸压力。 Bool value which let's users check if touch pressure is supported.
触摸支持 touchSupported 返回当前正在运行应用程序的设备是否支持触摸输入。 Returns whether the device on which application is currently running supports touch input.

Static Methods 静态方法

获取加速度事件 GetAccelerationEvent 返回在上一帧期间发生的特定加速度测量。(不分配临时变量)。 Returns specific acceleration measurement which occurred during last frame. (Does not allocate temporary variables).
获取轴 GetAxis 返回由axisName标识的虚拟轴的值。 Returns the value of the virtual axis identified by axisName.
获取轴(未加工的:[0,1]直接获取边缘值,没有补全中间值 GetAxisRaw 返回由axisName标识的虚拟轴的值,不应用平滑过滤。 Returns the value of the virtual axis identified by axisName with no smoothing filtering applied.
获取按钮 GetButton 按住buttonName标识的虚拟按钮时,返回true。 Returns true while the virtual button identified by buttonName is held down.
获取按钮按下 GetButtonDown 在用户按下由buttonName标识的虚拟按钮的帧期间,返回true。 Returns true during the frame the user pressed down the virtual button identified by buttonName.
获取按钮抬起 GetButtonUp 当用户释放由buttonName标识的虚拟按钮的第一帧时,返回true。 Returns true the first frame the user releases the virtual button identified by buttonName.
获得操纵杆名称 GetJoystickNames 返回描述连接的操纵杆的字符串数组。 Returns an array of strings describing the connected joysticks.
获取键值 GetKey 当用户按住名称标识的键时,返回true。 Returns true while the user holds down the key identified by name.
获取键值按下 GetKeyDown 在用户开始按下名称标识的帧期间,返回true。 Returns true during the frame the user starts pressing down the key identified by name.
获取键值抬起 GetKeyUp 在用户释放由名称标识的密钥的帧期间,返回true。 Returns true during the frame the user releases the key identified by name.
获取鼠标按钮 GetMouseButton 返回是否按住给定的鼠标按钮。 Returns whether the given mouse button is held down.
获取鼠标按钮按下 GetMouseButtonDown 在用户按下给定的鼠标按钮的帧期间返回true。 Returns true during the frame the user pressed the given mouse button.
获取鼠标按钮抬起 GetMouseButtonUp 在用户释放给定鼠标键的帧期间,返回true。 Returns true during the frame the user releases the given mouse button.
获取触摸 GetTouch 调用Input.GetTouch以获取Touch结构。 Call Input.GetTouch to obtain a Touch struct.
是否操纵预杆配置 IsJoystickPreconfigured 确定Unity是否已预先配置了特定的操纵杆模型。(仅Linux)。 Determine whether a particular joystick model has been preconfigured by Unity. (Linux-only).
重置输入轴 ResetInputAxes 重置所有输入。在ResetInputAxes之后,一帧中所有轴都返回0,所有按钮都返回0。 Resets all input. After ResetInputAxes all axes return to 0 and all buttons return to 0 for one frame.