> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/touch-screen-controls.md). Agents can also search and read this documentation through its [MCP server](https://docs.decentraland.org/~gitbook/mcp).

# 屏幕控制

为你的场景配置原生的屏幕触控控制。

在移动客户端上，玩家通过一组屏幕原生控件与场景互动——虚拟摇杆、准星和一组游戏手柄按钮。 `TouchScreenControls`  组件可以让场景重新调整这套 HUD：精简界面、隐藏摇杆或准星、更改中央大按钮的功能、用自定义图标替换按钮图形，或者彻底隐藏按钮并改用自定义 UI。

<figure><img src="https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-15146380df30e490d11a9f8ddc16b25c14999586%2Ftouch-controls-default.webp?alt=media" alt="The default mobile HUD: virtual joystick on the left, and on the right the large jump button with the interaction and E buttons beside it"><figcaption><p>自定义前的默认屏幕控件：跳跃、交互和 E</p></figcaption></figure>

{% hint style="info" %}
玩家处于场景中时，组件会自动应用；玩家离开时，客户端会立即恢复自身控件。它仅影响带有原生屏幕控件的平台：在桌面端不会产生任何效果，在 VR 中也不起作用。
{% endhint %}

## 默认显示内容

从 SDK `7.32.0` 开始，每个场景默认显示三个游戏手柄按钮：

* `IA_JUMP`，中央大按钮
* `IA_POINTER`，交互按钮
* `IA_PRIMARY`，E 按钮

`IA_SECONDARY` 以及 `IA_ACTION_3` 到 `IA_ACTION_6` 默认隐藏。只有当你需要不同于此默认配置的内容时，才需要使用 `TouchScreenControls` 。

{% hint style="warning" %}
**📔 注意**：默认显示三个按钮以及 `showDefault()` 辅助函数需要 SDK `7.32.0` 或更高版本。在更早版本中，除非场景隐藏部分按钮，否则八个按钮都会显示；并且不存在 `showDefault()` ，因此 `showAll()` 是恢复显示按钮的唯一方法。
{% endhint %}

SDK 会在场景启动时、第一帧渲染前写入这组默认设置一次；如果场景已自行选择按钮，则会跳过。符合以下任一情况，就视为场景已自行选择：

* 已调用某个按钮可见性辅助函数： `hideAll()`, `showAll()`, `showDefault()` 或 `hide()`。例如，在 `index.ts`的顶层调用其中一个函数，而不是在 `main()`.
* 根实体上的组件已列出至少一个按钮，例如场景在 `createOrReplace` 中设置了该组件，且调用位于 `index.ts`的顶层，而不是在 `main()`.

`hideJoystick()`, `showJoystick()`, `hideCrosshair()`, `showCrosshair()` 和 `setMainAction()` 不计入：默认按钮集仍会同时生效。

你的 `main()` 函数会在该写入之后运行，因此你在其中执行的任何操作都会覆盖默认按钮集。请在 `main()` 或之后调用这些辅助函数，而不要在顶层调用，否则默认按钮集会被跳过。像 `hide()` 这样的辅助函数会与默认设置合并，而 `createOrReplace` 则会用你的列表替换整个列表。

若要恢复已隐藏的按钮，请调用 `showAll()` 以显示全部八个按钮。 `showDefault()` 会恢复默认按钮集，同时保留你的图标、操纵杆、准星和主要操作按钮。

```ts
import { TouchScreenControls } from '@dcl/sdk/ecs'

export function main() {
	// 显示所有屏幕按钮，包括 1 / 2 / 3 / 4 按钮
	TouchScreenControls.showAll()
}
```

{% hint style="warning" %}
通过 `PointerEvents` 或 `inputSystem` 响应 F 或 1–4 **不会** 显示按钮。如果场景中使用了这些按键，请通过 `TouchScreenControls`启用对应按钮，否则移动端玩家将无法使用它们。
{% endhint %}

{% hint style="info" %}
移除该组件的场景（`TouchScreenControls.deleteFrom(engine.RootEntity)`）会显示全部八个按钮。
{% endhint %}

## 按钮布局的工作方式

游戏手柄按钮构成一个单一的 **优先级堆栈**。顺序固定如下：

1. `IA_JUMP`
2. `IA_POINTER`
3. `IA_PRIMARY` （E）
4. `IA_SECONDARY` （F）
5. `IA_ACTION_3` (1)
6. `IA_ACTION_4` (2)
7. `IA_ACTION_5` (3)
8. `IA_ACTION_6` (4)

屏幕上的位置也是固定的。 **可见的** 按钮会从堆栈顶部开始依次填入这些位置——因此你更改的是 *哪些* 按钮可见，以及 *哪个按钮会跳转*，而不是它们的顺序。

| 当你……                      | 这些控件……                                                                                  |
| ------------------------- | --------------------------------------------------------------------------------------- |
| **隐藏一个按钮** （任意按钮，包括跳跃按钮）  | 每个优先级较低的按钮都会上移填补空位。隐藏跳跃按钮后， `IA_POINTER` 会占据中央位置。                                       |
| **不做任何更改**                | 应用默认设置： `IA_JUMP` 是中央的大按钮， `IA_POINTER` 和 `IA_PRIMARY` 则位于周围的槽位中。                       |
| **设置主按钮** 使用 `mainAction` | 该操作会将按钮移到最前面并使其成为中央按钮；其他按钮仍保持原有顺序。                                                      |
| **将隐藏的按钮设为主按钮**           | 隐藏设置优先——该按钮会保持隐藏状态。这也包括默认隐藏的 F 和 1–4；同时也要启用该按钮（参见 **更改主按钮** ，位于 [常见任务](#common-tasks)). |
| **保持可见按钮不超过 5 个**         | 所有按钮都会直接显示（中央按钮加上周围最多四个按钮）；不会出现“+”菜单。                                                   |
| **保持可见按钮多于 5 个**          | “+”会占据最后一个槽位，因此有四个按钮会直接显示（中央按钮加上周围三个），其余按钮则收纳在“+”溢出切换按钮后面。                              |

<figure><img src="https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-2c1bf3ebb2a4c72f91fbd85fd13e64e823f94260%2Fcontrol-ordering.webp?alt=media" alt="Three HUDs showing the gamepad reflowing as the number of visible buttons changes"><figcaption><p>按钮会根据可见数量重新排列。 <strong>左侧（7 个按钮）：</strong> 主按钮、由 1–3 组成的弧形排列，以及一个“+”按钮；溢出的按钮（4–7）在一列中向上排列。 <strong>中间（5 个按钮）：</strong> 相同的弧形排列，溢出列更短（4–5），收纳在“+”按钮后面。 <strong>右侧（4 个按钮）：</strong> 四个按钮全部直接显示，“+”按钮消失。</p></figcaption></figure>

{% hint style="info" %}
这些 `1`/`2`/`3`/`4` 按钮默认处于隐藏状态。 `showAll()` 会将它们全部显示出来，但这样可见按钮就会达到八个，因此优先级最低的按钮会被收纳到“+”按钮后面。若要让编号按钮直接显示在屏幕上，请将可见按钮数量控制在五个或更少：只显示场景实际用到的按钮。
{% endhint %}

## 常见任务

`TouchScreenControls` 提供了一组便捷辅助函数。每个函数都会将组件写入场景的 `RootEntity` （客户端会从这里读取组件），并与当前值合并。在 `main()` 中或之后调用：在 `index.ts` 它们会在写入默认集合之前运行，因此默认集合会被跳过。

**更改主按钮** — 让中央大按钮触发其他操作：

```ts
import { TouchScreenControls, InputAction } from '@dcl/sdk/ecs'

export function main() {
	TouchScreenControls.setMainAction(InputAction.IA_PRIMARY)
}
```

`setMainAction()` 不会启用按钮。若要将 F 或某个数字按钮设为主按钮，也必须启用它，否则它会保持隐藏：

```ts
import { TouchScreenControls, InputAction } from '@dcl/sdk/ecs'

export function main() {
	TouchScreenControls.showAll()
	TouchScreenControls.hide([
		InputAction.IA_ACTION_3,
		InputAction.IA_ACTION_4,
		InputAction.IA_ACTION_5,
		InputAction.IA_ACTION_6,
	])
	TouchScreenControls.setMainAction(InputAction.IA_SECONDARY)
}
```

**隐藏摇杆或准星** — 移除移动摇杆和/或瞄准准星，并使用对应的 `show` 方法将它们重新显示：

```ts
TouchScreenControls.hideJoystick()
TouchScreenControls.hideCrosshair()

// 并使用以下方法重新显示：
TouchScreenControls.showJoystick()
TouchScreenControls.showCrosshair()
```

**隐藏指定按钮** — 传入要隐藏的操作（其余按钮会依次补位）。此调用会在已隐藏按钮的基础上继续隐藏；因此，使用默认按钮组时，这会让交互按钮成为中央按钮，并让 E 显示在它旁边：

```ts
import { TouchScreenControls, InputAction } from '@dcl/sdk/ecs'

export function main() {
	TouchScreenControls.hide([InputAction.IA_JUMP])
}
```

**使用 F 或数字按钮** — 这些按钮默认处于隐藏状态。显示所有按钮，然后隐藏场景中不会用到的按钮。 `showAll()` 也会清除自定义图标。如果使用 `createOrReplace`设置图标，请在 `showAll()` 之后、 `hide()`之前执行，因为 `createOrReplace` 会替换整个按钮列表，包括隐藏的按钮：

```ts
import { TouchScreenControls, InputAction } from '@dcl/sdk/ecs'

export function main() {
	// 显示 F 和 1，隐藏 2–4
	TouchScreenControls.showAll()
	TouchScreenControls.hide([InputAction.IA_ACTION_4, InputAction.IA_ACTION_5, InputAction.IA_ACTION_6])
}
```

**隐藏所有按钮、显示所有按钮，或恢复默认按钮组：**

```ts
import { TouchScreenControls } from '@dcl/sdk/ecs'

export function main() {
	// 选择一个：
	TouchScreenControls.hideAll()     // 不显示任何游戏手柄按钮
	TouchScreenControls.showAll()     // 显示全部八个按钮
	TouchScreenControls.showDefault() // 仅显示跳跃、指针和主按钮
}
```

这三个方法只影响游戏手柄按钮。它们不会恢复已隐藏的摇杆或准星，也不会更改主操作；如需恢复这些控件，请使用 `showJoystick()` / `showCrosshair()` 。 `hideAll()` 和 `showDefault()` 保留自定义图标，但 `showAll()` 会清除这些图标。

**替换按钮图标** ——如需完全控制（自定义图标、一次进行多项更改），请在 `engine.RootEntity`上直接编写原始组件。这会替换默认设置，因此未列出的按钮都会显示；之后调用 `showDefault()` 可再次隐藏 F 和 1–4，同时保留自定义图标：

```ts
import { engine, TouchScreenControls, InputAction } from '@dcl/sdk/ecs'

export function main() {
	TouchScreenControls.createOrReplace(engine.RootEntity, {
		hideJoystick: false,
		hideCrosshair: true,
		mainAction: InputAction.IA_PRIMARY,
		touchInputs: [
			{
				inputAction: InputAction.IA_PRIMARY,
				hide: false,
				icon: { tex: { $case: 'texture', texture: { src: 'images/grab.png' } } },
			},
		],
	})
	TouchScreenControls.showDefault()
}
```

辅助方法一览：

| 辅助方法                    | 功能                                                           |
| ----------------------- | ------------------------------------------------------------ |
| `setMainAction(action)` | 设置中央大按钮触发的操作。                                                |
| `hideJoystick()`        | 隐藏原生虚拟摇杆。                                                    |
| `showJoystick()`        | 再次显示原生虚拟摇杆。                                                  |
| `hideCrosshair()`       | 隐藏屏幕上的准星。                                                    |
| `showCrosshair()`       | 再次显示屏幕上的准星。                                                  |
| `hide(actions)`         | 隐藏指定的游戏手柄按钮（合并到当前配置中）。                                       |
| `hideAll()`             | 隐藏所有游戏手柄按钮。                                                  |
| `showAll()`             | 显示全部八个游戏手柄按钮，并清除每个按钮的所有单独设置（包括自定义图标）。 **不会** 影响摇杆或准星。        |
| `showDefault()`         | 恢复默认设置：跳跃、指针和主操作按钮显示，辅助操作按钮及操作 3 到 6 隐藏。图标、摇杆、准星和主操作设置均保持不变。 |

## 属性

使用 `createOrReplace`. `时，直接设置以下属性：`, `hideJoystick`, `hideCrosshair` touchInputs `以及每个条目中的 hide` 由 TypeScript 类型要求：

| 属性              | 类型                                                                                                                                                     | 说明                                                                                                                                                      |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `hideJoystick`  | *boolean*                                                                                                                                              | 隐藏原生虚拟移动摇杆。                                                                                                                                             |
| `hideCrosshair` | *boolean*                                                                                                                                              | 隐藏屏幕上的准星。                                                                                                                                               |
| `mainAction`    | [*InputAction*](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md#pointer-buttons) | 将此操作移到堆栈最前面，使其成为中间的大按钮；其他按钮保持原有顺序。仅游戏手柄操作有效（见下文）。未设置时，由第一个可见按钮（`IA_JUMP` （默认））作为主按钮。隐藏的按钮不会显示，包括 F 和 1–4。请参阅 [按钮布局的工作方式](#how-the-button-layout-works). |
| `touchInputs`   | *数组*                                                                                                                                                   | 逐个按钮覆盖设置。写入此字段会替换整个列表，因此，未列出的按钮会显示其默认图标，包括默认隐藏的按钮。                                                                                                      |

每个 `touchInputs` 条目包含：

| 字段            | 类型                                                                                                                                                     | 说明                                                                                                                                                                                                         |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputAction` | [*InputAction*](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md#pointer-buttons) | 此条目配置的屏幕按钮。                                                                                                                                                                                                |
| `hide`        | *boolean*                                                                                                                                              | 隐藏此按钮。默认值为 `false` （显示）。任何按钮都可以隐藏， **包括 `IA_JUMP`** ——其余按钮会依次上移填补空位。                                                                                                                                       |
| `icon`        | [*TextureUnion*](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/2d-ui/ui_background.md#background) （可选）                      | 使用场景图像覆盖按钮图标。请使用带有内容映射 `texture` 变体的 `src` （场景中包含的图像）—— `{ tex: { $case: 'texture', texture: { src: 'images/grab.png' } } }`。仅支持场景内容路径（不支持外部 URL、头像或视频纹理）。对于跳跃按钮，这会替换其所有动态状态（跳跃/二段跳/滑翔）。如果无法解析该路径，则使用内置图标。 |

## 哪些操作映射到哪些按钮

此处的 [`InputAction`](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md#pointer-buttons) 值与以下位置使用的值相同： [移动设备上的输入](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/input-on-mobile.md) 和 [点击事件](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md)。以下操作会映射到屏幕按钮：

| InputAction                                                   | 屏幕按钮             |
| ------------------------------------------------------------- | ---------------- |
| `IA_JUMP`                                                     | 中间的大按钮（默认）       |
| `IA_POINTER`                                                  | 交互按钮             |
| `IA_PRIMARY`                                                  | E 按钮             |
| `IA_SECONDARY`                                                | F 按钮             |
| `IA_ACTION_3` / `IA_ACTION_4` / `IA_ACTION_5` / `IA_ACTION_6` | 1 / 2 / 3 / 4 按钮 |

{% hint style="warning" %}
`IA_ANY` 和 `IA_MODIFIER` 是元值——它们不对应任何按钮，因此不能在此处使用。
{% endhint %}

## 示例

要隐藏摇杆、显示 F 按钮、隐藏数字按钮，并为中央跳跃按钮设置场景中提供的自定义图标：

```ts
import { engine, TouchScreenControls, InputAction } from '@dcl/sdk/ecs'

export function main() {
	TouchScreenControls.createOrReplace(engine.RootEntity, {
		hideJoystick: true,
		hideCrosshair: false,
		touchInputs: [
			// 此列表会替换默认列表，因此必须在此列出数字按钮
			// 才能将它们保持隐藏
			{ inputAction: InputAction.IA_SECONDARY, hide: false },
			{ inputAction: InputAction.IA_ACTION_3, hide: true },
			{ inputAction: InputAction.IA_ACTION_4, hide: true },
			{ inputAction: InputAction.IA_ACTION_5, hide: true },
			{ inputAction: InputAction.IA_ACTION_6, hide: true },
			{
				inputAction: InputAction.IA_JUMP,
				hide: false,
				icon: { tex: { $case: 'texture', texture: { src: 'images/banana.png' } } },
			},
		],
	})
}
```

<figure><img src="https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-e8fa39c0bb21eb9c2ff7949a101321cc1549f937%2Fcustom-main-action.webp?alt=media" alt="A mobile HUD with the joystick hidden, the E and F buttons beside the interaction button, and the central jump button showing a custom banana icon"><figcaption><p>结果：摇杆已隐藏，F 按钮已显示，数字按钮已隐藏（因此“+”也消失），跳跃按钮已更换图标</p></figcaption></figure>

要完全替换原生控件，请在此处将它们隐藏，然后使用以下内容创建自己的触控按钮： [UI 输入绑定](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/2d-ui/ui_input_binding.md).

## 相关内容

* [UI 输入绑定](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/2d-ui/ui_input_binding.md)
* [移动设备上的输入](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/input-on-mobile.md)
* [点击事件](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md)
* [通过代码检测平台](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/detect-platform.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/touch-screen-controls.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
