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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat: add UI component schemas, hierarchy submenu, and docs grouping
esengine17ed1612f056
diff --git a/docs/astro/astro.config.mjs b/docs/astro/astro.config.mjsindex e4918f60d..dd45de3f0 100644--- a/docs/astro/astro.config.mjs+++ b/docs/astro/astro.config.mjs@@ -76,7 +76,13 @@ export default defineConfig({},{ label: 'Scenes', translations: { 'zh-CN': '场景' }, slug: 'guides/scenes' },{ label: 'Input Handling', translations: { 'zh-CN': '输入处理' }, slug: 'guides/input' },- { label: 'UI & Text', translations: { 'zh-CN': 'UI 与文本' }, slug: 'guides/ui' },+ {+ label: 'UI',+ translations: { 'zh-CN': 'UI' },+ items: [+ { label: 'UI & Text', translations: { 'zh-CN': 'UI 与文本' }, slug: 'guides/ui' },+ ],+ },{ label: 'Asset Loading', translations: { 'zh-CN': '资源加载' }, slug: 'guides/assets' },{ label: 'Materials & Shaders', translations: { 'zh-CN': '材质与着色器' }, slug: 'guides/materials' },{ label: 'Geometry & Meshes', translations: { 'zh-CN': '几何体与网格' }, slug: 'guides/geometry' },diff --git a/docs/astro/src/content/docs/core-concepts/components.mdx b/docs/astro/src/content/docs/core-concepts/components.mdxindex 8204f04d6..085baf728 100644--- a/docs/astro/src/content/docs/core-concepts/components.mdx+++ b/docs/astro/src/content/docs/core-concepts/components.mdx@@ -109,12 +109,15 @@ UI text rendering. Works with `UIRect` and `Sprite` to display text. See [UI & T### UIRect-UI layout rectangle. Defines size, anchor, and pivot for UI elements.+UI layout rectangle. Defines anchored layout for UI elements.| Property | Type | Description ||----------|------|-------------|+| `anchorMin` | Vec2 | Lower-left anchor (0–1) |+| `anchorMax` | Vec2 | Upper-right anchor (0–1) |+| `offsetMin` | Vec2 | Pixel offset from anchorMin |+| `offsetMax` | Vec2 | Pixel offset from anchorMax || `size` | Vec2 | Width and height |-| `anchor` | Vec2 | Anchor point (0–1) || `pivot` | Vec2 | Pivot point for rotation |### UIMask@@ -125,6 +128,27 @@ Clips child entities to the parent's `UIRect` bounds. Requires `UIRect` and `Loc|----------|------|---------|-------------|| `enabled` | boolean | `true` | Whether clipping is active |+### Interactable++Marks a UI element as interactive (receives pointer events). Requires `UIRect`.++| Property | Type | Default | Description |+|----------|------|---------|-------------|+| `enabled` | boolean | `true` | Whether interaction is active |++### Button++Button behavior for UI elements. Requires `Interactable` and `UIRect`.++| Property | Type | Default | Description |+|----------|------|---------|-------------|+| `state` | number | `0` (Normal) | 0=Normal, 1=Hovered, 2=Pressed, 3=Disabled |+| `transition` | ButtonTransition \| null | `null` | Color transition for each state |++### ScreenSpace++Tag component that marks an entity as a screen-space UI root. Children of a ScreenSpace entity are laid out in screen coordinates via their `UIRect`. Requires `UIRect` and `LocalTransform`.+### NameA string identifier for the entity. Useful for finding entities by name at runtime.diff --git a/docs/astro/src/content/docs/zh/core-concepts/components.mdx b/docs/astro/src/content/docs/zh/core-concepts/components.mdxindex 9e0480e1f..5bb54b000 100644--- a/docs/astro/src/content/docs/zh/core-concepts/components.mdx+++ b/docs/astro/src/content/docs/zh/core-concepts/components.mdx@@ -109,12 +109,15 @@ UI 文本渲染。配合 `UIRect` 和 `Sprite` 组件显示文本。详见 [UI### UIRect-UI 布局矩形。定义 UI 元素的尺寸、锚点和旋转中心。+UI 布局矩形。定义 UI 元素的锚点布局。| 属性 | 类型 | 说明 ||------|------|------|+| `anchorMin` | Vec2 | 左下锚点(0–1) |+| `anchorMax` | Vec2 | 右上锚点(0–1) |+| `offsetMin` | Vec2 | 相对 anchorMin 的像素偏移 |+| `offsetMax` | Vec2 | 相对 anchorMax 的像素偏移 || `size` | Vec2 | 宽高 |-| `anchor` | Vec2 | 锚点(0–1) || `pivot` | Vec2 | 旋转中心点 |### UIMask@@ -125,6 +128,27 @@ UI 布局矩形。定义 UI 元素的尺寸、锚点和旋转中心。|------|------|--------|------|| `enabled` | boolean | `true` | 是否启用裁剪 |+### Interactable++将 UI 元素标记为可交互(接收指针事件)。需要 `UIRect`。++| 属性 | 类型 | 默认值 | 说明 |+|------|------|--------|------|+| `enabled` | boolean | `true` | 是否启用交互 |++### Button++UI 元素的按钮行为。需要 `Interactable` 和 `UIRect`。++| 属性 | 类型 | 默认值 | 说明 |+|------|------|--------|------|+| `state` | number | `0`(Normal) | 0=正常, 1=悬停, 2=按下, 3=禁用 |+| `transition` | ButtonTransition \| null | `null` | 各状态的颜色过渡 |++### ScreenSpace++标签组件,将实体标记为屏幕空间 UI 根节点。ScreenSpace 实体的子级通过 `UIRect` 在屏幕坐标中布局。需要 `UIRect` 和 `LocalTransform`。+### Name实体的字符串标识符,用于在运行时按名称查找实体。diff --git a/editor/src/panels/AddComponentPopup.ts b/editor/src/panels/AddComponentPopup.tsindex 0620a79bb..45a82023d 100644--- a/editor/src/panels/AddComponentPopup.ts+++ b/editor/src/panels/AddComponentPopup.ts@@ -35,6 +35,7 @@ export class AddComponentPopup {private listContainer_: HTMLElement | null = null;private categoryStates_: Map<ComponentCategory, CategoryState> = new Map([['builtin', { expanded: true }],+ ['ui', { expanded: true }],['physics', { expanded: true }],['script', { expanded: true }],['tag', { expanded: true }],@@ -123,6 +124,7 @@ export class AddComponentPopup {};html += renderCategory('builtin', 'Built-in', components.builtin, () => icons.box(14));+ html += renderCategory('ui', 'UI', components.ui, () => icons.pointer(14));html += renderCategory('physics', 'Physics', components.physics, () => icons.circle(14));html += renderCategory('script', 'Scripts', components.script, () => icons.cog(14));html += renderCategory('tag', 'Tags', components.tag, () => icons.tag(14));diff --git a/editor/src/panels/HierarchyPanel.ts b/editor/src/panels/HierarchyPanel.tsindex b1a1f82b1..03088e750 100644--- a/editor/src/panels/HierarchyPanel.ts+++ b/editor/src/panels/HierarchyPanel.ts@@ -538,6 +538,21 @@ export class HierarchyPanel {const entityData = entity !== null ? this.store_.getEntityData(entity as number) : null;const has = (type: string) => entityData?.components.some(c => c.type === type) ?? false;+ const uiChildren: ContextMenuItem[] = [];+ if (entity !== null) {+ uiChildren.push(+ { label: 'Add Interactable', disabled: has('Interactable'), onClick: () => this.addComponentToEntity(entity, 'Interactable') },+ { label: 'Add Button', disabled: has('Button'), onClick: () => this.addComponentToEntity(entity, 'Button') },+ { label: 'Add ScreenSpace', disabled: has('ScreenSpace'), onClick: () => this.addComponentToEntity(entity, 'ScreenSpace') },+ { label: '', separator: true },+ );+ }+ uiChildren.push(+ { label: 'New Button', onClick: () => this.createButtonEntity(entity) },+ { label: 'New Panel', onClick: () => this.createPanelEntity(entity) },+ { label: 'New ScreenSpace Root', onClick: () => this.createScreenSpaceRootEntity(entity) },+ );+const physicsChildren: ContextMenuItem[] = [];if (entity !== null) {physicsChildren.push(@@ -565,6 +580,7 @@ export class HierarchyPanel {{ label: 'Create Spine', icon: icons.bone(14), onClick: () => this.createEntityWithComponent('SpineAnimation', entity) },{ label: 'Create Camera', icon: icons.camera(14), onClick: () => this.createEntityWithComponent('Camera', entity) },{ label: 'Create Canvas', icon: icons.template(14), onClick: () => this.createEntityWithComponent('Canvas', entity) },+ { label: 'UI', icon: icons.pointer(14), children: uiChildren },{ label: 'Physics', icon: icons.circle(14), children: physicsChildren },{ label: '', separator: true },];@@ -602,6 +618,34 @@ export class HierarchyPanel {this.store_.addComponent(newEntity, colliderType, getDefaultComponentData(colliderType));}+ private createButtonEntity(parent: Entity | null): void {+ const newEntity = this.store_.createEntity('Button', parent);+ this.store_.addComponent(newEntity, 'LocalTransform', getDefaultComponentData('LocalTransform'));+ this.store_.addComponent(newEntity, 'Sprite', getDefaultComponentData('Sprite'));+ this.store_.addComponent(newEntity, 'UIRect', getDefaultComponentData('UIRect'));+ this.store_.addComponent(newEntity, 'Interactable', getDefaultComponentData('Interactable'));+ this.store_.addComponent(newEntity, 'Button', getDefaultComponentData('Button'));+ }++ private createPanelEntity(parent: Entity | null): void {+ const newEntity = this.store_.createEntity('Panel', parent);+ this.store_.addComponent(newEntity, 'LocalTransform', getDefaultComponentData('LocalTransform'));+ this.store_.addComponent(newEntity, 'Sprite', getDefaultComponentData('Sprite'));+ this.store_.addComponent(newEntity, 'UIRect', getDefaultComponentData('UIRect'));+ this.store_.addComponent(newEntity, 'UIMask', getDefaultComponentData('UIMask'));+ }++ private createScreenSpaceRootEntity(parent: Entity | null): void {+ const newEntity = this.store_.createEntity('ScreenSpace Root', parent);+ this.store_.addComponent(newEntity, 'LocalTransform', getDefaultComponentData('LocalTransform'));+ this.store_.addComponent(newEntity, 'UIRect', {+ ...getDefaultComponentData('UIRect'),+ anchorMin: { x: 0, y: 0 },+ anchorMax: { x: 1, y: 1 },+ });+ this.store_.addComponent(newEntity, 'ScreenSpace', {});+ }+private duplicateEntity(entity: Entity): void {const entityData = this.store_.getEntityData(entity as number);if (!entityData) return;diff --git a/editor/src/schemas/ComponentSchemas.ts b/editor/src/schemas/ComponentSchemas.tsindex 986d1145d..31460c12f 100644--- a/editor/src/schemas/ComponentSchemas.ts+++ b/editor/src/schemas/ComponentSchemas.ts@@ -10,7 +10,7 @@ import { getComponentDefaults } from 'esengine';// Types// =============================================================================-export type ComponentCategory = 'builtin' | 'physics' | 'script' | 'tag';+export type ComponentCategory = 'builtin' | 'ui' | 'physics' | 'script' | 'tag';export interface ComponentSchema {name: string;@@ -118,7 +118,7 @@ export const CameraSchema: ComponentSchema = {export const TextSchema: ComponentSchema = {name: 'Text',- category: 'builtin',+ category: 'ui',properties: [{ name: 'content', type: 'string' },{ name: 'fontFamily', type: 'font' },@@ -158,7 +158,7 @@ export const TextSchema: ComponentSchema = {export const UIRectSchema: ComponentSchema = {name: 'UIRect',- category: 'builtin',+ category: 'ui',properties: [{ name: '*', type: 'uirect' },],@@ -188,7 +188,7 @@ export const CanvasSchema: ComponentSchema = {export const BitmapTextSchema: ComponentSchema = {name: 'BitmapText',- category: 'builtin',+ category: 'ui',properties: [{ name: 'text', type: 'string' },{ name: 'font', type: 'bitmap-font-file' },@@ -210,12 +210,43 @@ export const BitmapTextSchema: ComponentSchema = {export const UIMaskSchema: ComponentSchema = {name: 'UIMask',- category: 'builtin',+ category: 'ui',+ properties: [+ { name: 'enabled', type: 'boolean' },+ ],+};++export const InteractableSchema: ComponentSchema = {+ name: 'Interactable',+ category: 'ui',properties: [{ name: 'enabled', type: 'boolean' },],};+export const ButtonSchema: ComponentSchema = {+ name: 'Button',+ category: 'ui',+ properties: [+ {+ name: 'state',+ type: 'enum',+ options: [+ { label: 'Normal', value: 0 },+ { label: 'Hovered', value: 1 },+ { label: 'Pressed', value: 2 },+ { label: 'Disabled', value: 3 },+ ],+ },+ ],+};++export const ScreenSpaceSchema: ComponentSchema = {+ name: 'ScreenSpace',+ category: 'ui',+ properties: [],+};+export const SpineAnimationSchema: ComponentSchema = {name: 'SpineAnimation',category: 'builtin',@@ -319,17 +350,19 @@ export function isComponentRemovable(name: string): boolean {export interface ComponentsByCategory {builtin: ComponentSchema[];+ ui: ComponentSchema[];physics: ComponentSchema[];script: ComponentSchema[];tag: ComponentSchema[];}export function getComponentsByCategory(): ComponentsByCategory {- const result: ComponentsByCategory = { builtin: [], physics: [], script: [], tag: [] };+ const result: ComponentsByCategory = { builtin: [], ui: [], physics: [], script: [], tag: [] };for (const schema of schemaRegistry.values()) {result[schema.category].push(schema);}result.builtin.sort((a, b) => a.name.localeCompare(b.name));+ result.ui.sort((a, b) => a.name.localeCompare(b.name));result.physics.sort((a, b) => a.name.localeCompare(b.name));result.script.sort((a, b) => a.name.localeCompare(b.name));result.tag.sort((a, b) => a.name.localeCompare(b.name));@@ -406,6 +439,9 @@ export function registerBuiltinSchemas(options?: BuiltinSchemaOptions): void {registerComponentSchema(CanvasSchema);registerComponentSchema(BitmapTextSchema);registerComponentSchema(UIMaskSchema);+ registerComponentSchema(InteractableSchema);+ registerComponentSchema(ButtonSchema);+ registerComponentSchema(ScreenSpaceSchema);registerComponentSchema(RigidBodySchema);registerComponentSchema(BoxColliderSchema);registerComponentSchema(CircleColliderSchema);@@ -445,6 +481,13 @@ const editorOnlyDefaults: Record<string, Record<string, unknown>> = {UIMask: {enabled: true,},+ Interactable: {+ enabled: true,+ },+ Button: {+ state: 0,+ transition: null,+ },Text: {content: 'Text',fontFamily: 'Arial',