> 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/detect-platform.md).

# 检测平台

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

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

## 可用函数

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

* **`getPlatform()`** — 返回当前平台，值为 `'mobile' | 'desktop' | 'web' | null`。在探索器向场景报告平台信息之前，它会返回 `null` （通常发生在场景启动后不久）。
* **`isMobile()`** — 如果玩家使用移动客户端，则返回 `true` 。
* **`isDesktop()`** — 如果玩家使用桌面客户端，则返回 `true` 。
* **`isWeb()`** — 如果玩家使用 Web 客户端，则返回 `true` 。

{% 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()
  }
}
```

你也可以采用相同的模式来：

* 显示或隐藏针对不同输入方式设计的屏幕提示。
* 在移动设备上，将较小的可点击元素替换为较大的触控目标。
* 停用在移动设备上不易使用的输入绑定（请参阅 [移动设备上的输入](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-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':
    // mobile-specific behavior
    break
  case 'desktop':
    // desktop-specific behavior
    break
  case 'web':
    // web-specific behavior
    break
  case null:
    // platform not yet known
    break
}
```

## 相关内容

* [移动设备安全区域](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/safe-area.md)
* [移动端 UI 最佳实践](https://docs.decentraland.org/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/ui-best-practices.md)
* [屏幕 UI](https://docs.decentraland.org/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 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/detect-platform.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.
