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.

feat(editor): ExposeRef — external prefab refs become per-instance bindable slots (P2 UX)

yhh39932f5a28ae
diff --git a/desktop/src/i18n/messages/project.ts b/desktop/src/i18n/messages/project.ts
index 607c929d0..90f6286fe 100644
--- a/desktop/src/i18n/messages/project.ts
+++ b/desktop/src/i18n/messages/project.ts
@@ -114,9 +114,12 @@ export const projectMessages = defineMessages({
'proj.applyDiffRemoved': { en: 'removed', zh: '移除' },
'proj.applyLabel': { en: 'Apply', zh: '应用' },
'proj.appliedStructural': { en: 'Applied to {name}: {overrides} override(s), {added} added, {removed} removed', zh: '已应用到 {name}:{overrides} 项覆盖,新增 {added},移除 {removed}' },
- 'proj.prefabExternalRefsTitle': { en: 'Clear external references?', zh: '清除外部引用?' },
- 'proj.prefabExternalRefsBody': { en: '{count} component reference(s) point to entities outside this selection and will be cleared in the prefab.', zh: '有 {count} 处组件引用指向所选之外的实体,将在预制体中被清除。' },
- 'proj.prefabExternalRefsConfirm': { en: 'Create anyway', zh: '仍然创建' },
+ 'proj.prefabExternalRefsTitle': { en: 'Expose external references?', zh: '暴露外部引用?' },
+ 'proj.prefabExternalRefsBody': {
+ en: "{count} reference(s) point to entities outside this prefab. They'll be left unbound in the prefab — each instance can then bind them to a scene entity in the Inspector.",
+ zh: '有 {count} 处引用指向此预制体之外的实体。它们将在预制体中留空——之后每个实例可在细节面板中将其绑定到场景实体。',
+ },
+ 'proj.prefabExternalRefsConfirm': { en: 'Create Prefab', zh: '创建预制体' },
'proj.openedPrefab': { en: 'Editing prefab {name}', zh: '正在编辑预制体 {name}' },
'proj.savedPrefab': { en: 'Saved prefab {name}', zh: '已保存预制体 {name}' },
'proj.prefabModeNested': { en: 'Nested / variant prefabs can\'t be edited in place yet', zh: '暂不支持原位编辑嵌套 / 变体预制体' },
diff --git a/sdk/tests/scene-prefab.test.ts b/sdk/tests/scene-prefab.test.ts
index f355bef15..60791f264 100644
--- a/sdk/tests/scene-prefab.test.ts
+++ b/sdk/tests/scene-prefab.test.ts
@@ -37,6 +37,7 @@ import {
type PrefabInstanceDelta,
type PrefabInstanceEntry,
type ExtractEntity,
+ type ProcessedEntity,
} from '../src/prefab/index';
defineBuiltin('Transform', {
@@ -315,6 +316,35 @@ describe('Prefab scene loading (PF1)', () => {
expect(external).toEqual([{ entityId: 11, componentType: 'Chaser', field: 'target', target: 99 }]);
});
+ // ExposeRef: a prefab leaves an external ref UNBOUND, and an instance binds it
+ // to a scene entity outside the instance. That binding must survive save (collapse)
+ // + load (expand) as a plain override carrying the target's stable scene id.
+ it('an instance binding of an exposed entity-ref to an external scene entity round-trips', () => {
+ defineComponent('Chaser', { target: 0 }, { entityFields: ['target'] });
+ const base: PrefabData = {
+ version: '2', name: 'Linker', rootEntityId: '0',
+ entities: [
+ { prefabEntityId: '0', name: 'Linker', parent: null, children: [], visible: true,
+ components: [{ type: 'Chaser', data: { target: INVALID_ENTITY } }] },
+ ],
+ };
+ // The live instance has its exposed ref bound to scene entity 99 (outside the instance).
+ const instance: ProcessedEntity[] = [
+ { id: 5, prefabEntityId: '0', name: 'Linker', parent: null, children: [], visible: true,
+ components: [{ type: 'Chaser', data: { target: 99 } }] },
+ ];
+ const delta = collapseInstance(base, '@uuid:linker', instance);
+ // Captured verbatim — a raw scene id, NOT normalised to a prefab-local id.
+ expect(delta.overrides).toEqual([
+ { prefabEntityId: '0', type: 'property', componentType: 'Chaser', propertyName: 'target', value: 99 },
+ ]);
+ // Expand restores the binding — the instance still points at scene entity 99.
+ let n = 100;
+ const { entities } = expandInstance(base, delta, () => n++);
+ const linker = entities.find((e) => e.prefabEntityId === '0')!;
+ expect((linker.components.find((c) => c.type === 'Chaser')!.data as { target: number }).target).toBe(99);
+ });
+
it('loadSceneData (sync) skips prefab entries without throwing', () => {
createMockModule();
const world = new World();