---
title: 移动设备输入
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/input/legacy/mobile
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/input/legacy/mobile.md
last_updated: 2026-10-30
generated_at: 2026-11-18T13:28:17.922Z
locale: zh-cn
---

# 移动设备输入

> 用于访问移动端DevicesON的触摸屏、加速度计和位置数据的参考。

> **Important:**
>
> Page面介绍 输入管理器 System的一部分，这是Legacy功能，不建议用于新建Projects。对于新建 Project 中的移动设备输入，使用 [Input System 包](https://docs.unity3d.com/Packages/com.unity.inputsystem@latest?subfolder=/manual/index.html)。

***

ON Mobile端 Devices，[输入](/engine/6000.0/script-reference/unityengine/input.md)类允许访问触摸屏、加速度计和地理/位置输入。

通过[移动键盘](/engine/6000.0/manual/input/mobile-keyboard.md)访问 ON 移动端 Devices 的键盘。

## 多点触控屏幕##multi-touch-screen

iPhone、iPad 和 iPod Touch Devices 最多可以同时跟踪触摸屏幕的五根手指。您可以通过访问 [input.touches](/engine/6000.0/script-reference/unityengine/input/touches.md) 属性 Array 来检索最后一帧期间触摸屏幕的每个手指的状态。

Android DevicesON其轨道的手指数量没有统一Limit。相反，此属性因设备而异，可以是旧设备上的两次触摸，也可以是一些新设备上的五根手指。

每个手指触摸都由[输入触摸](/engine/6000.0/script-reference/unityengine/touch.md)数据结构表示

以下示例脚本会ON用户点击屏幕时发出Ray：

```lang-cs
using UnityEngine;

public class TouchInput :MonoBehaviour
{
    GameObject particle;

    void Update()
    {
        Foreach(输入中的 Touch touch.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);
                }
            }
        }
    }
}
```

### 鼠标模拟##mouse-simulation

顶部了原生触控SupportON，Unity Android 还提供鼠标模拟。可使用标准[输入](/engine/6000.0/script-reference/unityengine/input.md)类中的鼠标功能。请注意，Androidvices旨在Support多个点触控。使用鼠标功能将仅Support单一触摸。此外，移动端DevicesON的手指触摸可以从一个区域Move到另一个区域，并且两者之间没有移动。ON 移动端 Devices 的鼠标模拟将提供移动，因此与触摸输入非常不同。建议在早期 Development 期间使用鼠标模拟，但应尽快使用触摸输入。

### 加速度计##accelerometer

当移动端设备移动时，内置加速度计会Reports沿三维Space中三个主轴的线性Acceleration变化。硬件将沿每个轴的 Acceleration 直接报告为 G 力值。值为 1.0 表示沿给轴的加载约为 +1g，而值为 -1.0 表示 -1g。如果您将设备直立（Home 按钮位于底部），则 X 轴沿右侧为正，Y 轴直接向上为正，Z 轴指向您。

您可以通过访问 [input.acceleration](/engine/6000.0/script-reference/unityengine/input/acceleration.md) 属性来检索加速度计值。

以下是使用加速度计MoveObject的示例脚本：

```lang-cs
using UnityEngine;

public class Accelerometer :MonoBehaviour
{
    浮点速度 = 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);
    }
}
```

### 低通道过滤器##low-pass-filter

加速度计读数可能会有干扰和噪点。ON信号应用低过滤波可以对其进行平滑并消除频率噪点。

以下脚本展示了如何将低过滤波Apply用于加速度计读数：

```lang-cs
using UnityEngine;

public class LowPassFilterExample :MonoBehaviour
{
    float accelerometerUpdateInterval = 1.0f / 60.0f;
    float lowPassKernelWidthInSeconds = 1.0f;

    private 浮点 lowPassFilterFactor;
    private Vector3 lowPassValue = Vector3.zero;

    void Start()
    {
        lowPassFilterFactor = accelerometerUpdateInterval / lowPassKernelWidthInSeconds;
        lowPassValue = Input.acceleration;
    }

    private void 更新()
    {
        lowPassValue = LowPassFilterAccelerometer(lowPassValue);
    }

    Vector3 LowPassFilterAccelerometer(Vector3 prevValue)
    {
        Vector3 newValue = Vector3.Lerp(prevValue, Input.acceleration, lowPassFilterFactor);
        return newValue;
    }
}
```

`LowPassKernelWidthInSeconds` 的值越大，过滤值向当前输入样本收敛的速度越慢（反之亦然）。

### 读取加速度计When，我希望尽可能精确。我该怎么办？##i'd-like-as-much-precision-as-possible-when-reading-the-accelerometer.-what-should-i-do?

读取 [input.Acceleration](/engine/6000.0/script-reference/unityengine/input/acceleration.md) 变量不等于对硬件采样。简而言之，Unity 以 60Hz 的频率对硬件进行样本并将result存储在变量中。实际上，情况稍微复杂一点：如果在 CPU 负载很大的情况下，加速度计采样不会以一致的时间间隔进行。因result，System可能会在一帧期间上报 2 个样本，然后在Next一帧期间报告 1 个样本。

您可以访问加速度计在帧期间执行的 All 测量。以下代码将说明上一帧内收集的All加速度计事件的Simple平均值：

```lang-cs
public class AccelerationEvents : MonoBehaviour
{ 
    void Update()
    {
        GetAccelerometerValue();
    }

    Vector3 GetAccelerometerValue()
    {
        Vector3 acc = Vector3.zero;
        浮点 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;
    }
}
```
