快速开始
Suity 的运行时分为三层:
| 层 | 包 | 职责 |
|---|---|---|
| Runtime Core | @suity/runtime(已内联进引擎包) | 场景图、Prefab 展开、引用、生命周期、信号 |
| Engine | @suity/engine-pixi | 把场景文档映射为 Pixi 显示对象,提供组件 catalog 与渲染 |
| Modules | @suity/module-* | Actor、Tilemap 等扩展,注册组件 handler 与渲染器 |
引擎包把 pixi.js 作为 peer 依赖,应用需要自行安装 pixi.js v8,确保应用与场景加载器共享同一个 Pixi 实例。
bash
npm install pixi.js@^8.22.0 @suity/engine-pixi加载一个场景
loadSceneInstance(source, context?) 接受场景文档、Addressable Key/uid 或 URL,返回可查询的 SceneInstance:
ts
import { Application } from "pixi.js";
import { loadSceneInstance } from "@suity/engine-pixi";
const app = new Application();
await app.init({ background: "#0a0a0a", resizeTo: window });
document.body.appendChild(app.canvas);
const scene = await loadSceneInstance("scenes/main", { mutable: true });
app.stage.addChild(scene.root);
const player = scene.get("Player");
player?.container().position.set(100, 50);
app.ticker.add((ticker) => scene.update(ticker.deltaMS / 1000));要点:
mutable: true才允许运行时实例化 Prefab、删除运行时节点;只读场景仍支持查询、update与destroy。context.assets缺省时使用全局SuityAssetsSDK;context.catalog缺省为引擎内置 catalog,接入模块时应传入合并后的 catalog。scene.get(ref)按节点 ref 查询,getByUid(uid)按 uid 查询;getAll()包含 Prefab 展开后的子节点。
运行时实例化 Prefab
ts
const enemy = await scene.instantiatePrefab({
asset: "prefabs/enemy",
parentRef: "Enemies",
name: "Enemy1",
transform: { x: 100, y: 50 },
});运行时创建的实例不会写回场景文档。需要持久化修改时,应通过编辑器的 transaction API 完成。
部署构建
专属构建(Dedicated Build)生成 suity.app.json、资产清单与脚本 bundle,入口是 @suity/engine-pixi/app:
ts
import { createRuntimeApp } from "@suity/engine-pixi/app";
const handle = await createRuntimeApp({
mount: document.getElementById("app")!,
appConfigUrl: "/suity.app.json",
assetsManifestUrl: "/assets.manifest.json",
scripts: {
enabled: true,
manifestUrl: "/scripts/manifest.json",
bundleUrl: "/scripts/bundle.js",
},
});
// 运行时切换场景(可选)
handle.loadScene?.("scenes/level-2");createRuntimeApp 会创建 Pixi Application、解析启动配置、初始化音频 mixer,并在 resize 时重新计算 viewport。详见 App Entry API。
下一步
- 理解场景、节点与组件
- 编写脚本组件
- 使用节点信号解耦逻辑
- 查阅 Runtime API
