包含此页的版本:
不含此页的版本:
重要提示:此页面记录了部分输入管理器设置(Input Manager Settings) 可在其中为项目定义所有不同的输入轴、按钮和控件。更多信息
请参阅术语表系统,这是一个遗留功能,不建议用于新项目。对于新项目中的移动设备输入,请使用输入系统包。
在移动设备上,Input 类提供对触摸屏、加速度计和地理/位置输入的访问。
通过移动键盘提供对移动设备键盘的访问。
iPhone、iPad 和 iPod Touch 设备能够跟踪多达五个手指同时触摸屏幕。您可以通过访问 Input.touches 属性数组来检索最后一帧期间触摸屏幕的每个手指的状态。
Android 设备对跟踪的手指数量没有统一的限制。相反,它因设备而异,可以是从旧设备上的两点触控到一些新设备上的五指。
每个手指触摸都由 Input.Touch 数据结构表示
以下示例脚本在用户点击屏幕时发射光线:
using UnityEngine;
public class TouchInput : MonoBehaviour
{
GameObject particle;
void Update()
{
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
// Construct a ray from the current touch coordinates
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray))
{
// Create a particle if hit
Instantiate(particle, transform.position, transform.rotation);
}
}
}
}
}
除了原生触摸支持之外,Unity iOS/Android 还提供了鼠标模拟。您可以使用标准 Input 类中的鼠标功能。请注意,iOS/Android 设备旨在支持多指触摸。使用鼠标功能将仅支持单指触摸。此外,移动设备上的手指触摸可以从一个区域移动到另一个区域,而它们之间没有移动。移动设备上的鼠标模拟将提供运动,因此与触摸输入相比有很大不同。建议在早期开发期间使用鼠标模拟,但尽快使用触摸输入。
当移动设备移动时,内置加速度计会报告线性加速度 三维空间中沿三个主轴的变化。加速度 沿每个轴的 G 力值由硬件直接报告为。一个值 1.0 表示沿给定轴的载荷约为 +1g,而值为 –1.0 代表 –1g。如果您将设备直立(主页按钮位于 bottom)在你面前,X轴沿右边是正的,Y轴是 正直接向上,Z轴正指向您。
可以通过访问 Input.acceleration 属性来检索加速度计值。
下面是一个示例脚本,它将使用加速度计移动对象:
using UnityEngine;
public class Accelerometer : MonoBehaviour
{
float speed = 10.0f;
void Update()
{
Vector3 dir = 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);
}
}
加速度计读数可能会不稳定且嘈杂。对信号应用低通滤波可以让您平滑它并消除高频噪声。
以下脚本演示如何将低通滤波应用于加速度计读数:
using UnityEngine;
public class LowPassFilterExample : MonoBehaviour
{
float accelerometerUpdateInterval = 1.0f / 60.0f;
float lowPassKernelWidthInSeconds = 1.0f;
private float lowPassFilterFactor;
private Vector3 lowPassValue = Vector3.zero;
void Start()
{
lowPassFilterFactor = accelerometerUpdateInterval / lowPassKernelWidthInSeconds;
lowPassValue = Input.acceleration;
}
private void Update()
{
lowPassValue = LowPassFilterAccelerometer(lowPassValue);
}
Vector3 LowPassFilterAccelerometer(Vector3 prevValue)
{
Vector3 newValue = Vector3.Lerp(prevValue, Input.acceleration, lowPassFilterFactor);
return newValue;
}
}
的值越大LowPassKernelWidthInSeconds,过滤后的值向当前输入样本收敛的速度越慢(反之亦然)。
读取 Input.acceleration 变量不等于对硬件进行采样。简而言之,Unity 以 60Hz 的频率对硬件进行采样,并将结果存储到变量中。实际上,事情有点复杂——如果在巨大的 CPU 负载下,加速度计采样不会以一致的时间间隔进行。因此,系统可能会在一帧内报告 2 个样本,然后在下一帧中报告 1 个样本。
您可以访问加速度计在帧期间执行的所有测量。以下代码将说明在最后一帧中收集的所有加速度计事件的简单平均值:
public class AccelerationEvents : MonoBehaviour
{
void Update()
{
GetAccelerometerValue();
}
Vector3 GetAccelerometerValue()
{
Vector3 acc = Vector3.zero;
float period = 0.0f;
foreach(AccelerationEvent evnt in Input.accelerationEvents)
{
acc += evnt.acceleration * evnt.deltaTime;
period += evnt.deltaTime;
}
if (period > 0)
{
acc *= 1.0f / period;
}
return acc;
}
}