> 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/wei-yi-dong-duan-gou-jian/kai-fa/detect-platform.md).

# 检测平台

检测你的场景是在移动端、桌面端还是网页端运行。

使用 `isMobile()` 用于专门针对移动端调整你的 UI、控件和玩法的函数。这是无需完全分叉场景逻辑即可在所有客户端上提供出色体验的推荐方式。

## 可用函数

```ts
import { getPlatform, isMobile, isDesktop, isWeb } from '@dcl/sdk/platform'
```

* **`getPlatform()`** —— 返回当前平台为 `'mobile' | 'desktop' | 'web' | null`。返回 `null` 直到浏览器将其平台回报给场景为止（这会在场景启动后不久发生）。
* **`isMobile()`** —— 返回 `真` 如果玩家在移动客户端上。
* **`isDesktop()`** —— 返回 `真` 如果玩家在桌面客户端上。
* **`isWeb()`** —— 返回 `真` 如果玩家在网页客户端上。

{% hint style="warning" %}
**📔 注意**：这些函数读取的值是在场景启动时异步填充的，无法保证到 `main()` 运行时它已经可用。如果你调用得太早，它们仍可能返回 `null` / `false`。为确保安全，请将任何依赖平台的设置延后，直到 `getPlatform()` 返回一个不同于 `null`的值为止，例如在系统中检查：

```ts
import { engine } from '@dcl/sdk/ecs'
import { getPlatform } from '@dcl/sdk/platform'

function platformCheckSystem() {
  if (getPlatform() === null) return
  engine.removeSystem(platformCheckSystem)
  setupUI()
}

engine.addSystem(platformCheckSystem)
```

{% endhint %}

## 按平台分支你的 UI

一种常见模式是为移动端和桌面端玩家设置不同的 UI：

```ts
import { isMobile, isDesktop } from '@dcl/sdk/platform'

function setupUI() {
  if (isMobile()) {
    // 更大的按钮，适合触控的更简洁布局
    createMobileUI()
  } else if (isDesktop()) {
    // 更密集的布局，针对键盘和鼠标优化
    createDesktopUI()
  }
}
```

你也可以使用同样的模式来：

* 显示或隐藏针对不同输入方式定制的屏幕指引。
* 在移动端用更大的触控目标替换较小的可点击元素。
* 禁用在移动端不易使用的输入绑定（见 [移动端输入](/creator/content-creator-zh/wei-yi-dong-duan-gou-jian/kai-fa/input-on-mobile.md)).

## 检查原始平台值

如果你需要在一个 switch 中处理多个平台，请使用 `getPlatform()`:

```ts
import { getPlatform } from '@dcl/sdk/platform'

const platform = getPlatform()

switch (platform) {
  case 'mobile':
    // 仅限移动端的行为
    break
  case 'desktop':
    // 仅限桌面端的行为
    break
  case 'web':
    // 仅限网页端的行为
    break
  case null:
    // 平台尚未知晓
    break
}
```

## 相关

* [移动端安全区域](/creator/content-creator-zh/wei-yi-dong-duan-gou-jian/kai-fa/safe-area.md)
* [移动端 UI 最佳实践](/creator/content-creator-zh/wei-yi-dong-duan-gou-jian/kai-fa/ui-best-practices.md)
* [屏幕内 UI](/creator/content-creator-zh/chang-jing-sdk7/2d-ui/onscreen-ui.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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.decentraland.org/creator/content-creator-zh/wei-yi-dong-duan-gou-jian/kai-fa/detect-platform.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
