> 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/mian-xiang-yi-dong-duan-gou-jian/kai-fa/ui-best-practices.md). Agents can also search and read this documentation through its [MCP server](https://docs.decentraland.org/~gitbook/mcp).

# UI 最佳实践

设计在移动端表现良好的场景 UI 的注意事项与禁忌。

目前还没有适用于 Decentraland 移动端 UI 的成熟方案——平台还很新，我们仍在不断迭代。本页的建议是当前的最佳实践，提炼自真实场景的开发经验。请将其作为起点，并在真实设备上进行测试。

{% embed url="<https://www.youtube.com/watch?v=5OmTTzpPdDc>" %}

## 建议

* **设计专为移动端打造的 UI**，或根据屏幕尺寸和平台调整 UI。使用 [`isMobile()`](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/detect-platform.md) 进行分支处理。
* **将关键 UI 放在** [**安全区域**](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/safe-area.md)**.** 设备的硬件边缘区域（刘海、状态栏、主屏幕指示条）默认已为你避开——只要不要通过 `screenInset: 'none'`选择退出。客户端自身的控件位于另一个区域，而 `screenInset: 'interactable'` 也能处理其中一部分：它会将 UI 放置在 Explorer 为场景 UI 指定的矩形区域内。在移动端，这会避开左侧控件；右下方的操作按钮则按设计覆盖在该区域上，因此请留意 [这些角落](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/safe-area.md#where-the-client-controls-live) 。 `'interactable'` 需要移动端客户端 `1.12.1` 或更新版本——旧版客户端不会报告任何边距，UI 会覆盖整个屏幕。
* **尽量减少选项。** 只显示玩家当前需要的内容，并逐步展示其余信息。
* **将需要玩家操作的对话框放在屏幕中央** ——适用于玩家需要阅读并作出响应的任何内容。
* **将无需玩家操作的消息放在顶部中央** ——例如状态、通知和环境信息。
* **在全屏 UI 显示期间隐藏触控控件。** 打开计分板、商店或结果面板时，玩家不需要跳跃或按 E。请参阅 [覆盖操作按钮的全屏 UI](#full-screen-ui-over-the-action-buttons).

## 注意事项

* **不要只按像素设置 UI 元素的大小，而不考虑虚拟屏幕。** 像素值会根据以下参考分辨率进行缩放： `virtualWidth` / `virtualHeight` ，详见 [屏幕 UI](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/2d-ui/onscreen-ui.md#screen-virtual-scale) —— `1600x720` 是移动端默认值， `1920x1080` 是桌面端和 Web 端默认值。请确认像素值所依据的参考分辨率；如果不是默认值，请显式传入，并搭配适配平台的尺寸设置。只有在确实需要原始画布像素时，才禁用虚拟屏幕（传入大小为 `0`）。
* **不要将元素放在安全区域之外。** 否则会与客户端控件冲突。
* **不要将 UI 锚定在左上角或左侧边缘。** 客户端会在场景 UI 上方的这个位置绘制聊天、个人资料和虚拟摇杆。请改为锚定在右侧或中央，或使用 `screenInset: 'interactable'`。请参阅 [切勿将 UI 锚定在左上角](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/ti-yan-she-ji/ux-ui-guide.md#never-anchor-your-ui-to-the-top-left).
* **不要在右下角放置可交互元素。** 操作按钮会覆盖该区域，在此处的点击会触发客户端按钮，而不是场景中的元素。
* **不要依赖尺寸过小的按钮。** 在触摸屏上，小目标很难可靠地点按。
* **不要将按键操作绑定到 `IA_SECONDARY` 或 `IA_ACTION_3`–`IA_ACTION_6`** （即 `F` 和 `1`/`2`/`3`/`4` 键盘上的按键），除非场景中显示了这些按钮。在移动设备上，这些按钮默认隐藏（SDK 7.32.0+）。请参阅 [移动设备上的输入](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/input-on-mobile.md).

## 覆盖操作按钮的全屏 UI

`screenInset: 'interactable'` 会让 UI 避开左侧控件，但右下角的操作按钮会按设计绘制在该区域上方。即使面板铺满整个区域，该处仍会被遮挡；在该区域点按会触发客户端按钮，而不是你的 UI。

对于 **游戏内 HUD** ，如果玩家移动时 HUD 仍须保持可用，请避开右下角：将其锚定在右上角或顶部中央。

对于 **会中断游戏过程的 UI**，例如记分板、商店或结果界面，打开时请隐藏触控控件，并在关闭时恢复：

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

function openScoreboard() {
  TouchScreenControls.hideAll()
  TouchScreenControls.hideJoystick()
  // ... show your UI
}

function closeScoreboard() {
  TouchScreenControls.showDefault()
  TouchScreenControls.showJoystick()
  // ... hide your UI
}
```

使用 `showDefault()` 而不是 `showAll()` 来恢复按钮：场景默认只显示跳跃、指针和主操作按钮，因此 `showAll()` 会重新显示玩家原本没有的五个按钮。三按钮默认设置和 `showDefault()` 都需要 SDK 7.32.0 或更高版本；在较旧的 SDK 中，八个按钮都会显示，而 `showAll()` 是恢复这些按钮的唯一方法。 `showDefault()` 只会恢复游戏手柄按钮，因此要将它与 `showJoystick()`配合使用。这两个辅助函数在桌面端都不起作用，因此无需按平台分支处理。完整组件请参阅 [屏幕控件](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/touch-screen-controls.md) 。

如果还要在面板打开时让化身停止移动，请再添加一个 [输入修饰器](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/tou-xiang/locomotion.md#freeze-the-player) 。

## 尺寸

过去的经验法则—— *先在桌面端设计，再将 UI 尺寸放大 3 倍以适配移动设备* ——早于 SDK 的两项改动，如今不应再不加判断地套用。现在，这个 3 倍放大效果大部分已由系统自动处理：

* **`devicePixelRatio` 不会影响 UI 布局。** 现在，按像素设定尺寸的 UI 渲染时会比以前大 `devicePixelRatio` 倍，通常为 **在手机上为 2–3 倍**，视网膜屏笔记本电脑上也一样。无法关闭此功能。
* **虚拟屏幕分辨率因平台而异，默认值为**, `1600x720` （移动设备）与 `1920x1080` （桌面设备和 Web），因此在手机上，相同的像素值本身就能覆盖约 1.2 倍的屏幕区域。

先沿用桌面端的尺寸，在真实设备上进行测量——请参阅 [在移动设备上预览](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/preview-on-mobile.md) ——仅在触控目标或文字实际显示不足时才放大。若确实需要采用不同设置，可使用 [`isMobile()`](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/detect-platform.md) 进行分支处理。

## 相关内容

* [缺失功能](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/yi-dong-ke-hu-duan/missing-features.md)
* [移动设备安全区域](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/safe-area.md)
* [在代码中检测平台](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/detect-platform.md)
* [屏幕内 UI](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/2d-ui/onscreen-ui.md)
* [UX 和 UI 指南](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/ti-yan-she-ji/ux-ui-guide.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/mian-xiang-yi-dong-duan-gou-jian/kai-fa/ui-best-practices.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.
