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 Scenes guide (Res(SceneManager))

yhh8e18c368d37d
diff --git a/docs/astro/astro.config.mjs b/docs/astro/astro.config.mjs
index 9a9fe491f..fb5c675a2 100644
--- a/docs/astro/astro.config.mjs
+++ b/docs/astro/astro.config.mjs
@@ -57,6 +57,7 @@ export default defineConfig({
{ label: 'Particles', translations: { 'zh-CN': '粒子' }, slug: 'guides/particle' },
{ label: 'Post-processing', translations: { 'zh-CN': '后处理' }, slug: 'guides/postprocess' },
{ label: 'Assets', translations: { 'zh-CN': '资源' }, slug: 'guides/assets' },
+ { label: 'Scenes', translations: { 'zh-CN': '场景' }, slug: 'guides/scene' },
{ label: 'WeChat MiniGame', translations: { 'zh-CN': '微信小游戏' }, slug: 'guides/wechat' },
],
},
diff --git a/docs/astro/src/content/docs/guides/scene.mdx b/docs/astro/src/content/docs/guides/scene.mdx
new file mode 100644
index 000000000..fd137a917
--- /dev/null
+++ b/docs/astro/src/content/docs/guides/scene.mdx
@@ -0,0 +1,48 @@
+---
+title: Scenes
+description: Load, switch, and stack scenes with Res(SceneManager).
+---
+
+import { Aside } from '@astrojs/starlight/components';
+
+A project is organized into scenes. They all share one ECS world; the
+`SceneManager` resource loads, switches, and unloads them, and gates each
+scene's systems to run only while it's active.
+
+## Switch scenes
+
+`switchTo` replaces the active scene (with an optional transition). It's async —
+`await` it:
+
+```typescript
+import { defineSystem, Res, SceneManager } from 'esengine';
+
+const goToLevel2 = defineSystem([Res(SceneManager)], async (scenes) => {
+ await scenes.switchTo('level2');
+});
+```
+
+## Additive scenes
+
+Load a scene **on top** of the current one — a HUD, a pause menu, a streamed
+region — without unloading what's underneath:
+
+```typescript
+await scenes.loadAdditive('pause-menu');
+// …later
+await scenes.unload('pause-menu');
+```
+
+## Query scene state
+
+- `scenes.getActive()` — the active scene name; `scenes.getActiveScenes()` for
+ all active (additive) scenes.
+- `scenes.isLoaded(name)`, `scenes.isActive(name)`.
+- `scenes.isTransitioning()` — true during a `switchTo` transition.
+
+<Aside type="note">
+ Because scenes share one world, an entity can outlive a scene switch if you
+ don't tag it to the scene. Systems declared in a scene only run while that
+ scene is active, so an additive HUD scene and the gameplay scene update
+ independently.
+</Aside>
diff --git a/docs/astro/src/content/docs/zh-cn/guides/scene.mdx b/docs/astro/src/content/docs/zh-cn/guides/scene.mdx
new file mode 100644
index 000000000..6cd5a24ae
--- /dev/null
+++ b/docs/astro/src/content/docs/zh-cn/guides/scene.mdx
@@ -0,0 +1,43 @@
+---
+title: 场景
+description: 用 Res(SceneManager) 加载、切换和叠加场景。
+---
+
+import { Aside } from '@astrojs/starlight/components';
+
+一个项目由若干场景组成。它们共享同一个 ECS 世界;`SceneManager` 资源负责加载、切换、
+卸载它们,并把每个场景的系统限制为只在该场景激活时运行。
+
+## 切换场景
+
+`switchTo` 会替换当前活动场景(可带过渡)。它是异步的——用 `await`:
+
+```typescript
+import { defineSystem, Res, SceneManager } from 'esengine';
+
+const goToLevel2 = defineSystem([Res(SceneManager)], async (scenes) => {
+ await scenes.switchTo('level2');
+});
+```
+
+## 叠加场景
+
+把一个场景加载到当前场景**之上**——HUD、暂停菜单、流式加载的区域——而不卸载下面的:
+
+```typescript
+await scenes.loadAdditive('pause-menu');
+// ……稍后
+await scenes.unload('pause-menu');
+```
+
+## 查询场景状态
+
+- `scenes.getActive()` —— 活动场景名;`scenes.getActiveScenes()` 返回所有活动
+ (叠加)场景。
+- `scenes.isLoaded(name)`、`scenes.isActive(name)`。
+- `scenes.isTransitioning()` —— `switchTo` 过渡期间为 true。
+
+<Aside type="note">
+ 由于场景共享同一个世界,如果你不把实体标记到场景上,它可能在场景切换后仍然存在。
+ 场景里声明的系统只在该场景激活时运行,所以叠加的 HUD 场景和游戏场景各自独立更新。
+</Aside>