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): a prefab can be created empty, not only extracted from an entity

yhh8c5433c56bbd
diff --git a/desktop/src/i18n/messages/content.ts b/desktop/src/i18n/messages/content.ts
index fc821c910..bf013992c 100644
--- a/desktop/src/i18n/messages/content.ts
+++ b/desktop/src/i18n/messages/content.ts
@@ -64,6 +64,7 @@ export const contentMessages = defineMessages({
'cb.menuImport': { en: 'Import…', zh: '导入…' },
'cb.menuNewFolder': { en: 'New Folder', zh: '新建文件夹' },
'cb.menuNewScene': { en: 'New Scene', zh: '新建场景' },
+ 'cb.menuNewPrefab': { en: 'New Prefab', zh: '新建预制体' },
'cb.menuNewScript': { en: 'New Script…', zh: '新建脚本…' },
'cb.menuNewAnimation': { en: 'New Animation', zh: '新建动画' },
'cb.menuNewInputMap': { en: 'New Input Map', zh: '新建输入映射' },
@@ -136,6 +137,7 @@ export const contentMessages = defineMessages({
'cb.moveFailed': { en: 'Move failed: {error}', zh: '移动失败:{error}' },
'cb.newFolderFailed': { en: 'New folder failed: {error}', zh: '新建文件夹失败:{error}' },
'cb.newSceneFailed': { en: 'New scene failed: {error}', zh: '新建场景失败:{error}' },
+ 'cb.newPrefabFailed': { en: 'New prefab failed: {error}', zh: '新建预制体失败:{error}' },
'cb.newInputMapFailed': { en: 'New input map failed: {error}', zh: '新建输入映射失败:{error}' },
'cb.newLocaleTableFailed': { en: 'New locale table failed: {error}', zh: '新建本地化表失败:{error}' },
'cb.revealFailed': { en: 'Couldn’t reveal: {error}', zh: '无法在资源管理器中显示:{error}' },
diff --git a/desktop/src/project/ProjectStore.ts b/desktop/src/project/ProjectStore.ts
index 273a96d5a..0169eadcf 100644
--- a/desktop/src/project/ProjectStore.ts
+++ b/desktop/src/project/ProjectStore.ts
@@ -671,6 +671,34 @@ class ProjectStoreImpl {
return window.estella.project.createAsset(destDir, 'strings.eslocale', content, 'locale');
}
+ /**
+ * A new, empty prefab: the entity the Create menu's "Empty" makes, as a
+ * standalone asset.
+ *
+ * Built from that entity SOURCE rather than a second definition of what an empty
+ * entity is — the same reason the Canvas hosting a UI prefab is built from the
+ * Canvas source. And run through `extractPrefab`, because that is what mints
+ * entity identities: a source's in-memory prefab carries `prefabEntityId: '0'`,
+ * the id every prefab had BEFORE stable ids, and an override aimed at `'0'` is
+ * refused now — so writing one to disk would author an asset nothing can
+ * customise.
+ */
+ private async blankPrefab(name: string): Promise<PrefabData> {
+ const built = await sourceById('empty')?.build?.({ parent: null });
+ const root = built?.entities[0];
+ if (!root) throw new Error('the "Empty" entity source is not registered');
+ const entity = { id: 0, name, parent: null, children: [], components: root.components, visible: true };
+ return extractPrefab([entity as unknown as ExtractEntity], 0, name);
+ }
+
+ /** Create a blank `.esprefab` under `destDir` — the Content Browser's New →
+ * Prefab. An empty prefab is a starting point: double-click opens it in Prefab
+ * Mode, where it is built up like a scene. */
+ async createPrefabFile(destDir: string, name = 'Prefab'): Promise<string> {
+ const content = JSON.stringify(await this.blankPrefab(name), null, 2) + '\n';
+ return window.estella.project.createAsset(destDir, 'prefab.esprefab', content, 'prefab');
+ }
+
/**
* Load the project's asset index (the main-process AssetDatabase scan)
* into a uuid→path registry, then point the engine
diff --git a/desktop/src/project/newAssetTypes.ts b/desktop/src/project/newAssetTypes.ts
index d9ecf4b36..ce92d21d7 100644
--- a/desktop/src/project/newAssetTypes.ts
+++ b/desktop/src/project/newAssetTypes.ts
@@ -43,6 +43,11 @@ const shaderTemplateMenu = (make: (dir: string, templateId: string) => Promise<v
export const NEW_ASSET_TYPES: NewAssetEntry[] = [
{ labelKey: 'cb.menuNewScene', create: (dir) => ProjectStore.createSceneFile(dir), errorKey: 'cb.newSceneFailed' },
+ // A blank-file creator, not an editor-backed one: entering Prefab Mode is a
+ // document swap, which is guarded by unsaved changes and would put a prompt in
+ // front of someone who asked for a file. So it lands in the browser and drops
+ // into rename, like New Scene; double-click opens it.
+ { labelKey: 'cb.menuNewPrefab', create: (dir) => ProjectStore.createPrefabFile(dir), errorKey: 'cb.newPrefabFailed' },
// Returns void like the editor-backed creators: a script's editor is the one
// configured for source files, which the dialog hands it to itself — and its
// name is fixed at creation (it is also an identifier and an import specifier),
diff --git a/desktop/tests/new-asset-types.test.ts b/desktop/tests/new-asset-types.test.ts
index ec9bcb36c..31bb5600b 100644
--- a/desktop/tests/new-asset-types.test.ts
+++ b/desktop/tests/new-asset-types.test.ts
@@ -37,6 +37,7 @@ describe('NEW_ASSET_TYPES', () => {
it('covers the expected creatable types', () => {
expect(NEW_ASSET_TYPES.map((a) => a.labelKey)).toEqual([
'cb.menuNewScene',
+ 'cb.menuNewPrefab',
'cb.menuNewScript',
'cb.menuNewAnimation',
'cb.menuNewInputMap',