Unity3D说明文档翻译-Mobile Device Input

来源:互联网 发布:js时间戳计算时间差 编辑:程序博客网 时间:2024/05/14 12:43

Mobile Device Input

手机设备输入

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.

适合于手机设备的键盘设置由”苹果键盘”提供.

Multi-Touch Screen

多点触控屏幕

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和iPad触屏设备可以追踪跟进5个手指同时触摸屏幕.你可以通过访问触屏输入属性数组检索最后一帧每个手指触摸屏幕的状态.

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:

每一个手指触摸代表一个触摸输入数据结构:

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.

系统撤销屏幕追踪,仅当(例如)用户把设备放他脸上或多于无个触摸同时发生.这是一个触摸的最终相位

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);

            }

        }

    }

}

 

 

Mouse Simulation

鼠标模拟

On top of native touch support Unity iOS/Android provides a mouse simulation. You can use mouse functionality from the standard Input class. Note that iOS/Android devices are designed to support multiple finger touch. Using the mouse functionality will support just a single finger touch. Also, finger touch on mobile devices can move from one area to another with no movement between them. Mouse simulation on mobile devices will provide movement, so is very different compared to touch input. The recommendation is to use the mouse simulation during early development but to use touch input as soon as possible.

Unity给iOS/Android本地鼠标模拟触屏支持.你可以从标准输入类使用鼠标功能.注意,iOS/Android设备被指派为支持手指触摸.使用鼠标功能将只支持单个手指触摸.同样,在手机设备上的手指触摸不需要移动即可从一个地方移动到另一个地方.鼠标模式在手机设备上将提供移动,所以和触屏输入非常不同.建议在早期开发时使用鼠标模拟但如果可以尽量使用触屏输入.

Accelerometer

加速计

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.如果你把设备竖直(使主按钮在底部)在你面前,往右是正x轴,向下是负y轴,朝向你的是负z轴.

You can retrieve the accelerometer value by accessing the Input.acceleration property.

你可以通过访问输入->加速度属性检索加速计的值.

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

//1)设备的XY平面映射到xz平面

// 2) rotated 90 degrees around Y axis

//2)围绕Y轴旋转90

    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...

//让它的移动从10米没秒替换为10米每帧

    dir *= Time.deltaTime;

 

// Move object

//移动对象

    transform.Translate (dir * speed);

}

 

 

Low-Pass Filter

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);

    return lowPassValue;

}

 

 

The greater the value of LowPassKernelWidthInSeconds, the slower the filtered value will converge towards the current input sample (and vice versa).

LowPassKernelWidthInSeconds的值越大过滤值相对于当前输入样本收敛得越慢(反之亦然).

I’d like as much precision as possible when reading the accelerometer. What should I do?

我想要尽可能精确的读取加速计.我该怎么做?

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.

读取输入->加速度变量不等于取样硬件.简言之,Unity以60Hz频率取样硬件并存储结果到变量.事实上,事情有点难懂-如果CPU超负荷加速计取样并不是总是发生在相同时间间隔.因此,系统可能报告两个样本在一个帧内,然后在下一帧又是1个样本.

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;

 

 

 

0 0