On mobile devices, the Input class offers access to touchscreen, accelerometer and geographical/location input.
Access to keyboard on mobile devices is provided via the iOS keyboard.
在移动设备中,Input类提供了对触屏、加速器、地理或定位输入的访问。访问移动设备的键盘是通过iOS keyboard类实现的。
The iPhone and iPod Touch devices are capable of tracking up to five fingers touching the screen simultaneously. You can retrieve the status of each finger touching the screen during the last frame by accessing the Input.touches property array.
iPhone和iPod触摸设备有同时追踪5个手指的能力。通过访问Input.touches 属性数组,可以取回每一个手指在上一帧触摸屏幕的状态。
Android devices don’t have a unified limit on how many fingers they track. Instead, it varies from device to device and can be anything from two-touch on older devices to five fingers on some newer devices.
Each finger touch is represented by an Input.Touch data structure:
安卓设备没有统一限制可以跟踪的手指数,它取决于设备,有的老设备支持2个触点,而新设备支持5个。每一个触摸是通过一个Input.Touch 结构体描述的。
Property: | Function: |
---|---|
fingerId手指id | The unique index for a touch.触摸的唯一索引。 |
position位置 | The screen position of the touch.触摸的屏幕位置。 |
deltaPosition位置偏移量 | The screen position change since the last frame.屏幕位置上一帧的变化量。 |
deltaTime时间变化量 | Amount of time that has passed since the last state change.上一次状态变化到现在的时间 |
tapCount点击数 | The iPhone/iPad screen is able to distinguish quick finger taps by the user. This counter will let you know how many times the user has tapped the screen without moving a finger to the sides. %android%Android devices do not count number of taps, this field is always 1.%%iPhone/iPad屏幕能够区分用户的快速点击数。这个计数让你知道原地点击屏幕的数目。安卓设备不统计点击数,这个值总为1. |
phase相位 | Describes so called “phase” or the state of the touch. It can help you determine if the touch just began, if user moved the finger or if he just lifted the finger.表示触摸的相位或状态。它帮你判断触摸是刚开始还是移动手指还是刚刚抬起手指。 |
Phase can be one of the following:
相位的值
Began开始 | A finger just touched the screen.手指刚接触屏幕 |
Moved移动 | A finger moved on the screen.手指在屏幕上移动 |
Stationary不动 | A finger is touching the screen but hasn’t moved since the last frame.手指接触屏幕,但是上一帧没有移动 |
Ended结束 | A finger was lifted from the screen. This is the final phase of a touch.手指抬离屏幕。这是触摸的最终相位 |
Canceled取消 | The system cancelled tracking for the touch, as when (for example) the user puts the device to her face or more than five touches happened simultaneously. This is the final phase of a touch.系统取消追踪触摸,比如用户把屏幕贴到脸上或者同时间超过5个触点。这是触摸的最终相位 |
Following is an example script which will shoot a ray whenever the user taps on the screen:
下面是一个用户点击屏幕发出射线的脚本例子。
var particle : GameObject; function Update () { for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Began) { // Construct a ray from the current touch coordinates用当前的触摸坐标构造一个射线
var ray = Camera.main.ScreenPointToRay (touch.position); if (Physics.Raycast (ray)) { // Create a particle if hit如果击中创建一个粒子 Instantiate (particle, transform.position, transform.rotation); } } } }
On top of native touch support Unity iOS/Android provides a mouse simulation. You can use mouse functionality from the standard Input class.
除了原有的触控外,Unity iOS/Android支持鼠标模拟。你可以从标准Input 类中使用鼠标功能。
As the mobile device moves, a built-in accelerometer reports linear acceleration changes along the three primary axes in three-dimensional space. 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.
当移动设备移动时,一个内置的加速计会报告三维空间3个主轴方向的线性加速度改变。每个轴的加速度直接由硬件以重力值的形式报告。1.0表示既定轴+1g加速度,-1.0表示-1g加速度。如果你竖直拿住设备(home键在下边),x轴正方向朝右,y轴正方向朝上,z轴正方向指向你。译者注:右手坐标系。
You can retrieve the accelerometer value by accessing the Input.acceleration property.通过Input.acceleration属性获得加速计的值
The following is an example script which will move an object using the accelerometer:下面是一个利用加速计移动物体的例子
var speed = 10.0;
function Update () {
var dir : Vector3 = Vector3.zero;
// we assume that the device is held parallel to the ground
// and the Home button is in the right hand
// remap the device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
// clamp acceleration vector to the unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// Move object
transform.Translate (dir * speed);
}
Accelerometer readings can be jerky and noisy. Applying low-pass filtering on the signal allows you to smooth it and get rid of high frequency noise.
The following script shows you how to apply low-pass filtering to accelerometer readings:
加速计的读数可能不稳定,使用低通滤波器处理信号可以使它更平滑,并且可以除去高平噪声。下面的的脚本展示如何对加速计读数使用低通滤波。
var AccelerometerUpdateInterval : float = 1.0 / 60.0; var LowPassKernelWidthInSeconds : float = 1.0; private var LowPassFilterFactor : float = AccelerometerUpdateInterval / LowPassKernelWidthInSeconds; // tweakable private var lowPassValue : Vector3 = Vector3.zero; function Start () { lowPassValue = Input.acceleration; } function LowPassFilterAccelerometer() : Vector3 { lowPassValue = Mathf.Lerp(lowPassValue, Input.acceleration, LowPassFilterFactor);//译者注:如果Input.acceleration突然变化,但是返回的是接近
//lowPassValue的值,也就是初始化的值,防止跳动。
return lowPassValue; }
The greater the value of LowPassKernelWidthInSeconds
, the slower the filtered value will converge towards the current input sample (and vice versa).
LowPassKernelWidthInSeconds 越大,向当前采样汇聚的越慢,反之亦然。
Reading the Input.acceleration variable does not equal sampling the hardware. Put simply, Unity samples the hardware at a frequency of 60Hz and stores the result into the variable. In reality, things are a little bit more complicated – accelerometer sampling doesn’t occur at consistent time intervals, if under significant CPU loads. As a result, the system might report 2 samples during one frame, then 1 sample during the next frame.
读取Input.acceleration 变量并不等同于硬件采样。简单地说,Unity以60赫兹对硬件采样并存储到这个变量里。而现实里,事情有点复杂--加速计采样并不是恒定时间间隔发生的,取决于cpu的负载。结果是,系统有可能一帧采两次样,下一帧里一次。
You can access all measurements executed by accelerometer during the frame. The following code will illustrate a simple average of all the accelerometer events that were collected within the last frame:
你可以访问加速计在一帧里执行的所有测量,下面的代码说明了上一帧收集的所有加速计事件的简单平均值的实现方法。
var period : float = 0.0; var acc : Vector3 = Vector3.zero; for (var evnt : iPhoneAccelerationEvent in iPhoneInput.accelerationEvents) { acc += evnt.acceleration * evnt.deltaTime; period += evnt.deltaTime; } if (period > 0) acc *= 1.0/period; return acc;
//译者注:类名可能有变化
var period : float = 0.0;
var acc :
Vector3 = Vector3.zero;
for (var evnt : AccelerationEvent in Input.accelerationEvents)
{
acc += evnt.acceleration * evnt.deltaTime; //加速度变化量的和
period += evnt.deltaTime;//时间变化量的和
}
if (period > 0)
acc *= 1.0/period;
return acc;