整理来自:Unity3d圣典
iOS和Android设备能够支持多点触控。你可以通过Input.touches属性集合访问在最近一帧中触摸在屏幕上的每一根手指的状态数据。
你可以读取Input.acceleration属性获得设备的加速度信息。你也可以使用Input.deviceOrientation属性获取设备在三维空间中的方位偏移。检测方位变化在你想要制作游戏行为中会非常有用,当用户转动设备或拿着设备时它是不同的。
当设备移动时,它们的加速感应器硬件将报告它们在三维空间中沿着三个主轴的线性加速变化数据。你可以使用这些数据检测设备当前的移动方向(相对于地面)和突然间的方向改变。
硬件沿着某感应一轴加速就会立即返回重力值。如果值为1.0代表沿着给定轴的方向+1g的重力加速度,如果值为-1.0代表-1g的重力加速度。如果你保持设备垂直(主页键在下方)在你正前方,那么X轴就是指向你右侧的方向,Y轴指向正上方,Z轴就是你所面向的方向。
Unity移动设备输入API是以苹果的iOS API为基础。学习一些原生API更有助于理解Unity的Input API。你可以查找苹果的Input API文档:
•Programming Guide:Event Handling (Apple iPhone SDK documentation)
•UITouch ClassReference (Apple iPhone SDK documentation)
Class Variables类变量
· isGyroAvailable
Returns a boolean value that indicates whether a gyroscope is available onthe device.
返回布尔值,陀螺仪在设备上是否可用。
· gyro
Returns default gyroscope. // 返回默认的陀螺仪。
· mousePosition
The current mouse position in pixel coordinates. (Read Only)
当前所在像素坐标的鼠标位置(只读)。
· anyKey
Is any key or mouse button currently held down? (Read Only)
是否有某一按键或鼠标按钮此时被按住?(只读)
· anyKeyDown
Returns true the first frame the user hits any key or mouse button (ReadOnly).
在第一帧用户按下某一按键或鼠标按钮,返回true(只读)
· inputString
Returns the keyboard input entered this frame (Read Only).
返回在这一帧的键盘输入(只读)
· acceleration
Last measured linear acceleration of a device in three-dimensional space(Read Only).
最近一次测量的设备在三维空间中的线性加速度(只读)
· accelerationEvents
Returns list of acceleration measurements which occurred during the lastframe (Read Only) (Allocates temporary variables).
返回上一帧测量的加速值数据列表(只读)(分配临时变量)
· accelerationEventCount
Number of acceleration measurements which occurred during last frame.
上一帧所进行的加速度测量次数。
· touches
Returns list of objects representing status of all touches during lastframe (Read Only) (Allocates temporary variables).
返回代表上一帧所有的触摸状态的对象列表(只读)(分配临时变量)
· touchCount
Number of touches (Read Only).
触摸的数量(只读)。
· eatKeyPressOnTextFieldFocus
Property indicating whether keypresses are eaten by a textinput if it hasfocus (default true).
该属性表示,如果具有焦点,是否通过文本输入框接收按键(默认为真)。
· multiTouchEnabled
Property indicating whether the system handles multiple touches.
此属性表明此系统是否支持多点触控。
· deviceOrientation
Device physical orientation as reported by OS (Read Only).
操作系统所报告的物理设备的方位信息(只读)
· imeCompositionMode
Controls enabling and disabling of IME input composition.
控制IME输入组合的启用和禁用。
· compositionString
The current IME composition string being typed by the user.
当前用户正在输入的IME组合字符串。
· compositionCursorPos
The current text input position used by IMEs to open windows.
当前文本输入位置,使用于IME来打开窗口。
Class Functions类函数
· GetAxis
Returns the value of the virtual axis identified by axisName.
根据坐标轴名称返回虚拟坐标系中的值。
· GetAxisRaw
Returns the value of the virtual axis identified by axisName with nosmoothing filtering applied.
通过坐标轴名称返回一个不使用平滑滤波器的虚拟坐标值。
· GetButton
Returns true while the virtual button identified by buttonName is helddown.
根据按钮名称返回true当对应的虚拟按钮被按住时。
· GetButtonDown
Returns true during the frame the user pressed down the virtual buttonidentified by buttonName.
在给定名称的虚拟按钮被按下的那一帧返回true。
· GetButtonUp
Returns true the first frame the user releases the virtual buttonidentified by buttonName.
在用户释放指定名称的虚拟按钮时返回true。
· GetKey
Returns true while the user holds down the key identified by name. Thinkauto fire.
当通过名称指定的按键被用户按住时返回true。想想自动开火。
· GetKeyDown
Returns true during the frame the user starts pressing down the keyidentified by name.
当用户按下指定名称的按键时的那一帧返回true。
· GetKeyUp
Returns true during the frame the user releases the key identified byname.
在用户释放给定名字的按键的那一帧返回true。
· GetJoystickNames
Returns an array of stings describing the connected joysticks.
返回一个用来描述已连接的控制杆的字符串集合。
· GetMouseButton
Returns whether the given mouse button is held down.
当指定的鼠标按钮被按下时返回true
· GetMouseButtonDown
Returns true during the frame the user pressed the given mouse button.
在用户按下指定鼠标按键的那一帧返回true。
· GetMouseButtonUp
Returns true during the frame the user releases the given mouse button.
在用户释放指定鼠标按键的那一帧返回true。
· ResetInputAxes
Resets all input. After ResetInputAxes all axes return to 0 and allbuttons return to 0 for one frame.
在一帧中重置所有的输入,重置输入指令之后所有的方向轴都被设置为0并且所有的按键都被设置为0。
· GetAccelerationEvent
Returns specific acceleration measurement which occurred during last frame(Does not allocate temporary variables).
返回上一帧发生的指定的加速度测量(不允许分配临时变量)
· GetTouch
Returns object representing status of a specific touch (Does not allocatetemporary variables).
返回一个存放触摸信息的对象(不允许分配临时变量)。
staticvar touches : Touch[]
Description描述
Returnslist of objects representing status of all touches during last frame (ReadOnly) (Allocates temporary variables).
返回代表上一帧所有的触摸状态的对象列表(只读)(分配临时变量)
Eachentry represents a status of a finger touching the screen.
每个记录都代表着一个手指在屏幕上的触碰状态
· C#
· JavaScript
// Prints number of fingers touching the screen
//输出触摸在屏幕上的手指数量
function Update () {
var fingerCount = 0;
for (var touch : Touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
}
if (fingerCount > 0)
print ("User has " + fingerCount + " finger(s) touching the screen");
}
Struct
Structuredescribing status of a finger touching the screen.
用来记录一个手指触摸在屏幕上的状态。
· fingerId
The unique index for touch.
触摸的唯一索引。
· position
The position of the touch.
触摸的位置。
· deltaPosition
The position delta since lastchange.
距离上次改变的距离增量。
· deltaTime
Amount of time passed sincelast change.
自上次改变的时间增量。
· tapCount
Number of taps.
点击次数。
· phase
Describes the phase of thetouch.
描述触摸的相位。直白点理解就是,记录触摸的阶段状态。
Enumeration
Describesphase of a finger touch.
描述手指触摸的状态。
· Began
A finger touched the screen.
手指已触摸屏幕。
· Moved
A finger moved on the screen.
手指在屏幕上移动。
· Stationary
A finger is touching thescreen but hasn't moved.
手指触摸屏幕,但并没有移动。
· Ended
A finger was lifted from thescreen. This is the final phase of a touch.
手指从屏幕上移开。这是一个触摸的最后状态。
· Canceled
The system cancelled trackingfor the touch, as when (for example) the user puts the device to her face ormore than five touches happened simultaneously. This is the final phase of atouch.
系统取消跟踪触摸,如用户把屏幕放到他脸上或超过五个接触同时发生。这是一个触摸的最后状态。
Interfaceinto the Gyroscope.
陀螺接口。
Usethis class to access gyroscope.
使用这个类访问陀螺。
· rotationRate
Returns rotation rate asmeasured by the device's gyroscope.
返回设备陀螺仪测量的旋转速率。
· rotationRateUnbiased
Returns unbiased rotationrate as measured by the device's gyroscope.
返回由设备陀螺仪测量的无偏旋转速率。
· gravity
Returns the gravityacceleration vector expressed in the device's reference frame.
返回在设备参考帧的重力加速度。
· userAcceleration
Returns the acceleration thatthe user is giving to the device.
返回用户提供给设备的加速度。
· attitude
Returns the attitude of thedevice. 返回设备的姿态。
· enabled
Sets or retrieves status ofthis gyroscope. 设置或检索该陀螺仪的状态。
· updateInterval
Sets or retrieves gyroscopeinterval in seconds.
设置或检索该陀螺仪的间隔,以秒为单位。