> 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/2d-ui/ui_input_binding.md).

# UI 输入绑定

将输入动作绑定到你自己的 UI 元素上，让它们驱动玩家输入。

`UiInputBinding` 可将任意 UI 元素变为控制按钮。将一个或多个 [`InputAction`](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md#pointer-buttons)绑定到元素上；按住该元素（通过触摸或指针）时，这些操作会保持按下状态，从而驱动本地玩家的输入（移动、跳跃）以及场景中的任何 [`InputAction`](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md#pointer-buttons) 监听器，效果与原生屏幕按钮完全相同。

可用它构建自定义触控控制。通常会搭配 [屏幕控制](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/touch-screen-controls.md)一起使用：隐藏原生按钮，然后在原位置放置自定义按钮。

<figure><img src="https://2460066822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoPnXBby9S6MrsW83Y9qZ%2Fuploads%2Fgit-blob-9124ebe50a4d4cd7ceae7b8f11b6efb34c6e25d8%2Fui-input-binding.webp?alt=media" alt="A retro-style gamepad assembled from custom UI elements, each wired to an input action"><figcaption><p>完全自定义的游戏手柄，由 UI 元素构成——每个按钮都是绑定到输入操作的 UI 实体</p></figcaption></figure>

## 将操作绑定到 UI 元素

在 `uiInputBinding` 属性添加到 `.tsx` UI 中的任意元素上，并列出按住该元素时要保持按下状态的操作。所有 UI 元素（[`UiEntity`](/creator/content-creator-zh/chang-jing-sdk7/2d-ui/onscreen-ui.md#ui-entities), [`Button`](/creator/content-creator-zh/chang-jing-sdk7/2d-ui/onscreen-ui.md#ui-entities), [`Label`](/creator/content-creator-zh/chang-jing-sdk7/2d-ui/onscreen-ui.md#ui-entities)等）均可使用此属性，就像 `uiTransform` 和 `uiBackground`.

**按住时让玩家向前移动的按钮：**

```tsx
import { InputAction } from '@dcl/sdk/ecs'
import { Button } from '@dcl/sdk/react-ecs'

export const forwardButton = () => (
	<Button
		value="▲"
		uiTransform={{ width: 100, height: 100 }}
		uiInputBinding={{ actions: [InputAction.IA_FORWARD] }}
	/>
)
```

## 构建自定义控制按键组

组合多个已绑定的元素，构成完整的控制方案。

**一个移动按钮，加上一个触发 `IA_PRIMARY`:**

```tsx
import { InputAction } from '@dcl/sdk/ecs'
import { Button, UiEntity } from '@dcl/sdk/react-ecs'

export const customControls = () => (
	<UiEntity uiTransform={{ width: 240, height: 120 }}>
		<Button
			value="▲"
			uiTransform={{ width: 100, height: 100 }}
			uiInputBinding={{ actions: [InputAction.IA_FORWARD] }}
		/>
		<Button
			value="使用"
			uiTransform={{ width: 100, height: 100 }}
			uiInputBinding={{ actions: [InputAction.IA_PRIMARY] }}
		/>
	</UiEntity>
)
```

绑定的操作与原生按钮的行为完全相同： `IA_FORWARD` / `IA_BACKWARD` / `IA_LEFT` / `IA_RIGHT` 会控制化身移动，而场景中的 [`InputAction`](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md#pointer-buttons) 监听器可以读取任何操作。移除该属性（或组件）即可解除绑定。

## 属性

| 属性        | 类型                                                                                                                                 | 描述                                                                                                                                                                                                                                                              |
| --------- | ---------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `actions` | 数组，元素类型为 [`InputAction`](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md#pointer-buttons) | 按住此元素时持续触发的输入操作。完整列表请参阅 [点击事件](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md) ，其中列出了所有 [`InputAction`](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.md#pointer-buttons) 取值。 |

{% hint style="info" %}
将其与 [`TouchScreenControls`](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/touch-screen-controls.md) 搭配使用可隐藏原生控件，并用自定义触控 UI 替换它们——这是发布完全自定义控制方案的推荐方式。
{% endhint %}

## 相关内容

* [屏幕控件](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/touch-screen-controls.md)
* [UI 按钮事件](/creator/content-creator-zh/chang-jing-sdk7/2d-ui/ui_button_events.md)
* [移动设备上的输入](/creator/content-creator-zh/mian-xiang-yi-dong-duan-gou-jian/kai-fa/input-on-mobile.md)
* [点击事件](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/an-niu-shi-jian/click-events.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/2d-ui/ui_input_binding.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.
