Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

docs: add prefab guide, TextInput docs, and prefab asset loading

esengineb6ce4958954b
diff --git a/docs/astro/astro.config.mjs b/docs/astro/astro.config.mjs
index b19c94889..4b9b833f4 100644
--- a/docs/astro/astro.config.mjs
+++ b/docs/astro/astro.config.mjs
@@ -75,6 +75,7 @@ export default defineConfig({
],
},
{ label: 'Scenes', translations: { 'zh-CN': '场景' }, slug: 'guides/scenes' },
+ { label: 'Prefabs', translations: { 'zh-CN': '预制体' }, slug: 'guides/prefabs' },
{ label: 'Input Handling', translations: { 'zh-CN': '输入处理' }, slug: 'guides/input' },
{
label: 'UI',
diff --git a/docs/astro/src/content/docs/guides/assets.mdx b/docs/astro/src/content/docs/guides/assets.mdx
index 03d228ab4..c67d6c173 100644
--- a/docs/astro/src/content/docs/guides/assets.mdx
+++ b/docs/astro/src/content/docs/guides/assets.mdx
@@ -92,6 +92,20 @@ Supports `.fnt` (BMFont text format) and `.bmfont` (JSON metadata) files. The lo
`loadAll` does not currently support bitmap fonts. Load them separately with `loadBitmapFont`.
</Aside>
+## Prefab Loading
+
+```typescript
+const prefab = await assets.loadPrefab('prefabs/Enemy.esprefab');
+```
+
+Loads and parses a `.esprefab` file. The result is a `PrefabData` object that can be passed to `instantiatePrefab()`.
+
+| Method | Returns | Description |
+|--------|---------|-------------|
+| `loadPrefab(path, baseUrl?)` | `Promise<PrefabData>` | Load and cache a prefab file |
+
+See [Prefabs](/microes/guides/prefabs/) for the full prefab API.
+
## Generic File Loading
| Method | Returns | Description |
diff --git a/docs/astro/src/content/docs/guides/prefabs.mdx b/docs/astro/src/content/docs/guides/prefabs.mdx
new file mode 100644
index 000000000..b2b50a4fd
--- /dev/null
+++ b/docs/astro/src/content/docs/guides/prefabs.mdx
@@ -0,0 +1,118 @@
+---
+title: Prefabs
+description: Reusable entity templates with override support in ESEngine
+---
+
+import { Aside } from '@astrojs/starlight/components';
+
+A **prefab** is a reusable entity template stored as a `.esprefab` file. You create prefabs from existing entities in the editor, then instantiate copies anywhere in your scene. Each instance can override individual properties while staying linked to the source prefab.
+
+## Editor Workflow
+
+### Creating a Prefab
+
+1. Set up an entity (or entity tree) in the hierarchy
+2. Right-click the root entity → **Prefab** → **Save as Prefab...**
+3. Choose a save location in the content browser
+
+Alternatively, drag an entity from the hierarchy into a content browser folder.
+
+The entity and its children become a **prefab instance** — linked to the saved `.esprefab` file.
+
+### Instantiating a Prefab
+
+Drag a `.esprefab` file from the content browser into the hierarchy panel. A new instance is created with all the original entities and components.
+
+### Editing a Prefab
+
+Double-click a `.esprefab` file in the content browser to enter **prefab edit mode**. The editor loads the prefab as a temporary scene where you can modify entities directly. Changes are saved back to the `.esprefab` file when you exit.
+
+### Overrides
+
+When you modify a property on a prefab instance, it becomes an **override**. Overridden properties are visually marked in the inspector.
+
+- **Apply to Prefab** — writes instance overrides back to the `.esprefab` file and updates all other instances
+- **Revert Prefab** — discards all overrides, restoring the instance to match the source prefab
+- **Unpack Prefab** — removes the prefab link, turning the instance into regular entities
+
+Access these via right-click → **Prefab** on a prefab root entity.
+
+## Runtime API
+
+Use the `Prefabs` resource to load and instantiate prefabs inside a system. It handles asset loading and entity creation in a single call:
+
+```typescript
+import { addStartupSystem, defineSystem, Res, Prefabs } from 'esengine';
+
+addStartupSystem(defineSystem(
+ [Res(Prefabs)],
+ async (prefabs) => {
+ const { root, entities } = await prefabs.instantiate('prefabs/Enemy.esprefab');
+ }
+));
+```
+
+### Options
+
+| Option | Type | Description |
+|--------|------|-------------|
+| `baseUrl` | string | Base URL for resolving asset paths |
+| `parent` | Entity | Parent entity to attach the prefab root to |
+| `overrides` | PrefabOverride[] | Property overrides to apply on instantiation |
+
+### Applying Overrides
+
+Pass overrides to customize instances at spawn time:
+
+```typescript
+const { root } = await prefabs.instantiate('prefabs/Enemy.esprefab', {
+ parent: parentEntity,
+ overrides: [
+ {
+ prefabEntityId: 1,
+ type: 'property',
+ componentType: 'LocalTransform',
+ propertyName: 'position',
+ value: { x: 200, y: 100, z: 0 },
+ },
+ ],
+});
+```
+
+### PrefabOverride
+
+| Field | Type | Description |
+|-------|------|-------------|
+| `prefabEntityId` | number | Target entity ID within the prefab |
+| `type` | string | `'property'` \| `'component_added'` \| `'component_removed'` \| `'name'` \| `'visibility'` |
+| `componentType` | string | Component to modify (for `property` and `component_removed`) |
+| `propertyName` | string | Property name (for `property`) |
+| `value` | unknown | New value (for `property`, `name`, `visibility`) |
+| `componentData` | object | Component type and data (for `component_added`) |
+
+### InstantiatePrefabResult
+
+| Field | Type | Description |
+|-------|------|-------------|
+| `root` | Entity | The root entity of the instantiated prefab |
+| `entities` | Map\<number, Entity\> | Mapping from prefab entity IDs to world entities |
+
+### Nested Prefabs
+
+Prefabs can contain references to other prefabs. When instantiating, nested prefabs are automatically loaded and flattened. This requires an `assetServer` in the options:
+
+```typescript
+const result = await instantiatePrefab(world, prefab, {
+ assetServer: assets,
+});
+```
+
+<Aside type="note">
+ Prefab files are cached by the asset server. Loading the same `.esprefab` path multiple times returns the cached result.
+</Aside>
+
+## Next Steps
+
+- [Scenes](/microes/guides/scenes/) — scene structure and entity hierarchy
+- [Asset Loading](/microes/guides/assets/) — loading textures and other assets
+- [Components](/microes/core-concepts/components/) — all builtin components
diff --git a/docs/astro/src/content/docs/guides/ui.mdx b/docs/astro/src/content/docs/guides/ui.mdx
index 8b008d0ff..ece3e34dc 100644
--- a/docs/astro/src/content/docs/guides/ui.mdx
+++ b/docs/astro/src/content/docs/guides/ui.mdx
@@ -268,6 +268,56 @@ world.insert(entity, UIRect, {
Both `UILayoutPlugin` and `UIInteractionPlugin` are registered automatically by `createWebApp()`. No manual setup is needed.
</Aside>
+## TextInput
+
+`TextInput` renders an editable text field. The `TextInputPlugin` handles keyboard input, cursor blinking, and texture rendering automatically.
+
+### Setup
+
+In the editor: create an entity via right-click → **Create** → **UI** → **TextInput**, or manually add `LocalTransform`, `UIRect`, `Interactable`, and `TextInput` components.
+
+<Aside type="note">
+ The plugin automatically adds a `Sprite` component if missing — no manual setup needed. `TextInputPlugin` is registered automatically by `createWebApp()`.
+</Aside>
+
+### TextInput Properties
+
+| Property | Type | Default | Description |
+|----------|------|---------|-------------|
+| `value` | string | `''` | Current text content |
+| `placeholder` | string | `''` | Hint text shown when empty |
+| `placeholderColor` | Color | `{r:0.6, g:0.6, b:0.6, a:1}` | Placeholder text color |
+| `fontFamily` | string | `'Arial'` | Font family name |
+| `fontSize` | number | `16` | Font size in pixels |
+| `color` | Color | `{r:1, g:1, b:1, a:1}` | Text color |
+| `backgroundColor` | Color | `{r:0.15, g:0.15, b:0.15, a:1}` | Background fill color |
+| `padding` | number | `6` | Padding in pixels |
+| `maxLength` | number | `0` | Max characters (0 = unlimited) |
+| `multiline` | boolean | `false` | Allow multi-line input |
+| `password` | boolean | `false` | Mask text with bullets |
+| `readOnly` | boolean | `false` | Prevent user input |
+
+The `focused`, `cursorPos`, and `dirty` properties are managed internally by the plugin.
+
+### Reading Input at Runtime
+
+```typescript
+import { defineSystem, addSystem, Query, Res } from 'esengine';
+import { TextInput, UIEvents } from 'esengine';
+
+addSystem(defineSystem(
+ [Query(TextInput), Res(UIEvents)],
+ (query, events) => {
+ for (const e of events.query('submit')) {
+ const [, input] = query.get(e.entity);
+ console.log('Submitted:', input.value);
+ }
+ }
+));
+```
+
+**TextInput events:** `change` (value changed), `submit` (Enter pressed in single-line mode)
+
## Next Steps
- [Components](/microes/core-concepts/components/) — all builtin components
diff --git a/docs/astro/src/content/docs/zh/guides/assets.mdx b/docs/astro/src/content/docs/zh/guides/assets.mdx
index ac5221193..319b54dcf 100644
--- a/docs/astro/src/content/docs/zh/guides/assets.mdx
+++ b/docs/astro/src/content/docs/zh/guides/assets.mdx
@@ -92,6 +92,20 @@ const fontHandle = await assets.loadBitmapFont('assets/my-font.fnt');
`loadAll` 目前不支持位图字体,需要使用 `loadBitmapFont` 单独加载。
</Aside>
+## 预制体加载
+
+```typescript
+const prefab = await assets.loadPrefab('prefabs/Enemy.esprefab');
+```
+
+加载并解析 `.esprefab` 文件。返回的 `PrefabData` 对象可传入 `instantiatePrefab()` 使用。
+
+| 方法 | 返回值 | 说明 |
+|------|--------|------|
+| `loadPrefab(path, baseUrl?)` | `Promise<PrefabData>` | 加载并缓存预制体文件 |
+
+详见[预制体](/microes/zh/guides/prefabs/)了解完整的预制体 API。
+
## 通用文件加载
| 方法 | 返回值 | 说明 |
diff --git a/docs/astro/src/content/docs/zh/guides/prefabs.mdx b/docs/astro/src/content/docs/zh/guides/prefabs.mdx
new file mode 100644
index 000000000..4a3ce2e24
--- /dev/null
+++ b/docs/astro/src/content/docs/zh/guides/prefabs.mdx
@@ -0,0 +1,118 @@
+---
+title: 预制体
+description: ESEngine 中可复用的实体模板,支持属性覆盖
+---
+
+import { Aside } from '@astrojs/starlight/components';
+
+**预制体(Prefab)** 是存储为 `.esprefab` 文件的可复用实体模板。你可以在编辑器中从现有实体创建预制体,然后在场景中实例化多个副本。每个实例可以覆盖单独的属性,同时保持与源预制体的关联。
+
+## 编辑器工作流
+
+### 创建预制体
+
+1. 在层级面板中搭建好实体(或实体树)
+2. 右键点击根实体 → **Prefab** → **Save as Prefab...**
+3. 在内容浏览器中选择保存位置
+
+也可以直接将实体从层级面板拖拽到内容浏览器的文件夹中。
+
+该实体及其子实体会变为**预制体实例** — 关联到保存的 `.esprefab` 文件。
+
+### 实例化预制体
+
+将 `.esprefab` 文件从内容浏览器拖拽到层级面板中,即可创建包含所有原始实体和组件的新实例。
+
+### 编辑预制体
+
+在内容浏览器中双击 `.esprefab` 文件进入**预制体编辑模式**。编辑器会将预制体加载为临时场景,你可以直接修改实体。退出时更改会保存回 `.esprefab` 文件。
+
+### 属性覆盖
+
+当你修改预制体实例上的属性时,该属性成为**覆盖值(Override)**。被覆盖的属性在检查器中会有视觉标记。
+
+- **Apply to Prefab** — 将实例的覆盖值写回 `.esprefab` 文件,并更新所有其他实例
+- **Revert Prefab** — 丢弃所有覆盖值,将实例恢复为与源预制体一致
+- **Unpack Prefab** — 移除预制体关联,将实例转为普通实体
+
+通过右键点击预制体根实体 → **Prefab** 访问这些操作。
+
+## 运行时 API
+
+使用 `Prefabs` 资源在系统中加载和实例化预制体,一次调用完成资源加载和实体创建:
+
+```typescript
+import { addStartupSystem, defineSystem, Res, Prefabs } from 'esengine';
+
+addStartupSystem(defineSystem(
+ [Res(Prefabs)],
+ async (prefabs) => {
+ const { root, entities } = await prefabs.instantiate('prefabs/Enemy.esprefab');
+ }
+));
+```
+
+### 选项
+
+| 选项 | 类型 | 说明 |
+|------|------|------|
+| `baseUrl` | string | 解析资源路径的基础 URL |
+| `parent` | Entity | 预制体根实体要挂载到的父实体 |
+| `overrides` | PrefabOverride[] | 实例化时应用的属性覆盖 |
+
+### 应用覆盖值
+
+通过传入覆盖值在生成时自定义实例:
+
+```typescript
+const { root } = await prefabs.instantiate('prefabs/Enemy.esprefab', {
+ parent: parentEntity,
+ overrides: [
+ {
+ prefabEntityId: 1,
+ type: 'property',
+ componentType: 'LocalTransform',
+ propertyName: 'position',
+ value: { x: 200, y: 100, z: 0 },
+ },
+ ],
+});
+```
+
+### PrefabOverride
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `prefabEntityId` | number | 预制体内的目标实体 ID |
+| `type` | string | `'property'` \| `'component_added'` \| `'component_removed'` \| `'name'` \| `'visibility'` |
+| `componentType` | string | 要修改的组件(用于 `property` 和 `component_removed`) |
+| `propertyName` | string | 属性名(用于 `property`) |
+| `value` | unknown | 新值(用于 `property`、`name`、`visibility`) |
+| `componentData` | object | 组件类型和数据(用于 `component_added`) |
+
+### InstantiatePrefabResult
+
+| 字段 | 类型 | 说明 |
+|------|------|------|
+| `root` | Entity | 实例化后的预制体根实体 |
+| `entities` | Map\<number, Entity\> | 预制体实体 ID 到世界实体的映射 |
+
+### 嵌套预制体
+
+预制体可以包含对其他预制体的引用。实例化时,嵌套预制体会被自动加载并展开。这需要在选项中提供 `assetServer`:
+
+```typescript
+const result = await instantiatePrefab(world, prefab, {
+ assetServer: assets,
+});
+```
+
+<Aside type="note">
+ 预制体文件由资源服务器缓存。多次加载同一 `.esprefab` 路径会返回缓存结果。
+</Aside>
+
+## 下一步
+
+- [场景](/microes/zh/guides/scenes/) — 场景结构和实体层级
+- [资源加载](/microes/zh/guides/assets/) — 加载纹理和其他资源
+- [组件](/microes/zh/core-concepts/components/) — 所有内置组件
diff --git a/docs/astro/src/content/docs/zh/guides/ui.mdx b/docs/astro/src/content/docs/zh/guides/ui.mdx
index 932f819f3..a2720ee28 100644
--- a/docs/astro/src/content/docs/zh/guides/ui.mdx
+++ b/docs/astro/src/content/docs/zh/guides/ui.mdx
@@ -268,6 +268,56 @@ world.insert(entity, UIRect, {
`UILayoutPlugin` 和 `UIInteractionPlugin` 均由 `createWebApp()` 自动注册,无需手动设置。
</Aside>
+## TextInput
+
+`TextInput` 渲染一个可编辑文本输入框。`TextInputPlugin` 自动处理键盘输入、光标闪烁和纹理渲染。
+
+### 设置步骤
+
+在编辑器中:通过右键 → **Create** → **UI** → **TextInput** 创建实体,或手动添加 `LocalTransform`、`UIRect`、`Interactable` 和 `TextInput` 组件。
+
+<Aside type="note">
+ 插件会在缺少 `Sprite` 组件时自动添加,无需手动设置。`TextInputPlugin` 由 `createWebApp()` 自动注册。
+</Aside>
+
+### TextInput 属性
+
+| 属性 | 类型 | 默认值 | 说明 |
+|------|------|--------|------|
+| `value` | string | `''` | 当前文本内容 |
+| `placeholder` | string | `''` | 为空时显示的提示文本 |
+| `placeholderColor` | Color | `{r:0.6, g:0.6, b:0.6, a:1}` | 提示文本颜色 |
+| `fontFamily` | string | `'Arial'` | 字体名称 |
+| `fontSize` | number | `16` | 字号(像素) |
+| `color` | Color | `{r:1, g:1, b:1, a:1}` | 文本颜色 |
+| `backgroundColor` | Color | `{r:0.15, g:0.15, b:0.15, a:1}` | 背景填充颜色 |
+| `padding` | number | `6` | 内边距(像素) |
+| `maxLength` | number | `0` | 最大字符数(0 = 不限) |
+| `multiline` | boolean | `false` | 允许多行输入 |
+| `password` | boolean | `false` | 用圆点遮蔽文本 |
+| `readOnly` | boolean | `false` | 禁止用户输入 |
+
+`focused`、`cursorPos` 和 `dirty` 属性由插件内部管理。
+
+### 运行时读取输入
+
+```typescript
+import { defineSystem, addSystem, Query, Res } from 'esengine';
+import { TextInput, UIEvents } from 'esengine';
+
+addSystem(defineSystem(
+ [Query(TextInput), Res(UIEvents)],
+ (query, events) => {
+ for (const e of events.query('submit')) {
+ const [, input] = query.get(e.entity);
+ console.log('提交内容:', input.value);
+ }
+ }
+));
+```
+
+**TextInput 事件:** `change`(值变更)、`submit`(单行模式下按回车)
+
## 下一步
- [组件](/microes/zh/core-concepts/components/) — 所有内置组件