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.

fix(runtime): expand prefab instances when a packaged scene loads

esenginec0e1a9a0a279
diff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.ts
index fbb1ec12b..774e5641d 100644
--- a/sdk/src/asset/Assets.ts
+++ b/sdk/src/asset/Assets.ts
@@ -1032,6 +1032,9 @@ export class Assets {
const counter = this.refCounter_;
for (const entity of sceneData.entities) {
+ // Prefab-instance entries carry no inline components (a prefab ref +
+ // overrides); their handles bind when the prefab expands, not here.
+ if (!Array.isArray(entity.components)) continue;
for (const comp of entity.components) {
// Single source: the component def's declared asset fields.
// Handle write-back always keys off assetFields (the direct
diff --git a/sdk/src/asset/discoverAssets.ts b/sdk/src/asset/discoverAssets.ts
index d16a8f2ab..b6d536772 100644
--- a/sdk/src/asset/discoverAssets.ts
+++ b/sdk/src/asset/discoverAssets.ts
@@ -90,6 +90,12 @@ export function discoverSceneAssets(
for (const entityData of sceneData.entities) {
if (entityData.visible === false) continue;
+ // A prefab-instance entry carries no inline `components` (it holds a prefab
+ // ref plus overrides); its assets are discovered when the prefab is expanded
+ // by loadSceneWithAssets, not from here. Skipping it also guards the loop
+ // against `components: undefined` — the discovery pass runs on the raw scene,
+ // before expansion, in the runtime and scene-manager preload paths.
+ if (!Array.isArray(entityData.components)) continue;
for (const compData of entityData.components) {
const comp = getComponent(compData.type);
diff --git a/sdk/src/runtimeLoader.ts b/sdk/src/runtimeLoader.ts
index 382175ab8..caf5e97e7 100644
--- a/sdk/src/runtimeLoader.ts
+++ b/sdk/src/runtimeLoader.ts
@@ -6,7 +6,8 @@
*/
import { SceneOwner } from './component';
-import { loadSceneData, updateCameraAspectRatio, type SceneData } from './scene';
+import { loadSceneData, updateCameraAspectRatio, sceneHasPrefabEntries, expandScenePrefabs, type SceneData } from './scene';
+import type { PrefabData } from './prefab/types';
import { switchTheme, resolveThemeTokens, type ThemeOverrides } from './ui';
import { discoverSceneAssets } from './asset/discoverAssets';
import type { ESEngineModule } from './wasm';
@@ -228,7 +229,7 @@ export function sceneUsesPhysics(sceneData: SceneData): boolean {
}
export async function loadRuntimeScene(options: LoadRuntimeSceneOptions): Promise<void> {
- const { app, module, sceneData, source, physicsConfig, physicsEnabled, uiTheme, uiThemeOverrides, sceneName } = options;
+ const { app, module, source, physicsConfig, physicsEnabled, uiTheme, uiThemeOverrides, sceneName } = options;
// The SpineManager is owned by SpinePlugin (built from the realm's
// app.sideModules host); read it from there so every realm — play / playable /
@@ -236,6 +237,26 @@ export async function loadRuntimeScene(options: LoadRuntimeSceneOptions): Promis
// wins for headless/tests.
const spineManager = options.spineManager ?? app.getPlugin(SpinePlugin)?.spineManager ?? null;
+ // Expand prefab instances up front: an exported scene keeps them as a prefab
+ // ref + overrides (the cook does not flatten them), and the rest of this
+ // pipeline — discovery, preload, the synchronous loadSceneData spawn — operates
+ // on plain entities and skips a prefab entry. This is the same expansion
+ // loadSceneWithAssets runs for the editor's play realm, so a packaged game
+ // spawns prefab instances exactly as Play does (play == ship), instead of
+ // silently dropping them.
+ const runtimeAssets = ensureRuntimeAssets(app, module, source);
+ let sceneData = options.sceneData;
+ if (sceneHasPrefabEntries(sceneData)) {
+ sceneData = await expandScenePrefabs(sceneData, async (ref) => {
+ try {
+ return ((await runtimeAssets.loadPrefab(ref))?.data as PrefabData) ?? null;
+ } catch (e) {
+ log.warn('scene', `Failed to load prefab "${ref}": ${e instanceof Error ? e.message : String(e)}`);
+ return null;
+ }
+ });
+ }
+
// Spine pairs (raw refs) for the two-phase spine load+apply below; every
// other asset type loads through the single canonical Assets channel.
const discovered = discoverSceneAssets(sceneData);
@@ -247,7 +268,7 @@ export async function loadRuntimeScene(options: LoadRuntimeSceneOptions): Promis
// that resolve `@uuid:` refs at runtime — e.g. the tilemap plugin — find the
// SAME resolver the preload keyed the caches with) and persists across scene
// loads. Spine stays a two-phase load+apply below (skipSpine).
- const sceneAssets = ensureRuntimeAssets(app, module, source);
+ const sceneAssets = runtimeAssets;
// Video source refs resolve through the SAME channel as every other asset
// (the editor's play realm wires this too) — a cooked build maps the
// authored logical ref (clip.mp4 / @uuid) to its staged .esv.
diff --git a/sdk/src/spine/loadSpineScene.ts b/sdk/src/spine/loadSpineScene.ts
index 9d847e81a..e749e0e1d 100644
--- a/sdk/src/spine/loadSpineScene.ts
+++ b/sdk/src/spine/loadSpineScene.ts
@@ -170,6 +170,9 @@ export async function applySpineEntities(opts: {
if (assetInfo.size === 0) return;
for (const sceneEntity of sceneData.entities) {
+ // Prefab instances carry no inline components; skip them (the async loader
+ // expands prefabs before this runs).
+ if (!Array.isArray(sceneEntity.components)) continue;
for (const comp of sceneEntity.components) {
const spineDesc = getComponentSpineFieldDescriptor(comp.type);
if (!spineDesc || !comp.data) continue;
diff --git a/sdk/tests/discoverAssets.test.ts b/sdk/tests/discoverAssets.test.ts
index 7c8207cc8..a4c407556 100644
--- a/sdk/tests/discoverAssets.test.ts
+++ b/sdk/tests/discoverAssets.test.ts
@@ -138,6 +138,22 @@ describe('discoverSceneAssets', () => {
expect(getAssetPathsByType(refs, 'texture').size).toBe(0);
});
+ it('skips prefab-instance entries (no inline components)', () => {
+ // The runtime and scene-manager preload paths run discovery on the raw
+ // scene, before loadSceneWithAssets expands prefabs — a prefab-instance
+ // entry carries a prefab ref + overrides but no `components` array, and
+ // iterating that undefined threw. Discovery must skip it, not crash.
+ const scene = makeScene([
+ { id: 1, name: 'coin', parent: null, prefab: 'assets/coin.esprefab',
+ overrides: {}, added: [], removed: [] } as unknown as SceneData['entities'][number],
+ { id: 2, name: 'e2', parent: null, children: [],
+ components: [{ type: SPRITE_NAME, data: { texture: 'p.png', material: '', color: { r: 1, g: 1, b: 1, a: 1 } } }] },
+ ]);
+
+ expect(() => discoverSceneAssets(scene)).not.toThrow();
+ expect(getAssetPathsByType(discoverSceneAssets(scene), 'texture')).toEqual(new Set(['p.png']));
+ });
+
it('skips empty string values', () => {
const scene = makeScene([{
id: 1, name: 'e1', parent: null, children: [],