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: add prefab support to asset system

esengineb399b7d71eee
diff --git a/editor/src/asset/AssetDatabase.ts b/editor/src/asset/AssetDatabase.ts
index e8b588e35..dd29a61a3 100644
--- a/editor/src/asset/AssetDatabase.ts
+++ b/editor/src/asset/AssetDatabase.ts
@@ -260,6 +260,10 @@ export class AssetDatabase {
for (const comp of entity.components || []) {
changed = await this.migrateComponentRefs(comp) || changed;
}
+ if (entity.prefab?.prefabPath && !isUUID(entity.prefab.prefabPath)) {
+ entity.prefab.prefabPath = await this.ensureMeta(entity.prefab.prefabPath);
+ changed = true;
+ }
}
if (scene.textureMetadata) {
@@ -288,6 +292,12 @@ export class AssetDatabase {
for (const comp of entity.components || []) {
this.resolveComponentRefs(comp);
}
+ if (entity.prefab?.prefabPath && isUUID(entity.prefab.prefabPath)) {
+ const path = this.getPath(entity.prefab.prefabPath);
+ if (path) {
+ entity.prefab.prefabPath = path;
+ }
+ }
}
if (scene.textureMetadata) {
diff --git a/editor/src/asset/AssetDependencyAnalyzer.ts b/editor/src/asset/AssetDependencyAnalyzer.ts
index 7e6a5ef70..b6913fe7f 100644
--- a/editor/src/asset/AssetDependencyAnalyzer.ts
+++ b/editor/src/asset/AssetDependencyAnalyzer.ts
@@ -159,6 +159,7 @@ export class AssetDependencyAnalyzer {
const refs: string[] = [];
const entities = scene.entities as Array<{
components: Array<{ type: string; data: Record<string, unknown> }>;
+ prefab?: { prefabPath: string };
}> | undefined;
if (!entities) return refs;
@@ -170,6 +171,9 @@ export class AssetDependencyAnalyzer {
}
for (const entity of entities) {
+ if (entity.prefab?.prefabPath) {
+ refs.push(entity.prefab.prefabPath);
+ }
for (const comp of entity.components || []) {
const scanner = scannerMap.get(comp.type);
if (scanner && comp.data) {
@@ -206,6 +210,8 @@ export class AssetDependencyAnalyzer {
await this.collectAtlasRefs(resolvedPath, uuid, dependencies, dependents);
} else if (resolvedPath.endsWith('.bmfont') || resolvedPath.endsWith('.fnt')) {
await this.collectFontRefs(resolvedPath, uuid, dependencies, dependents);
+ } else if (resolvedPath.endsWith('.esprefab')) {
+ await this.collectPrefabRefs(resolvedPath, uuid, dependencies, dependents, visited);
}
}
@@ -214,7 +220,7 @@ export class AssetDependencyAnalyzer {
materialUuid: string,
dependencies: Map<string, Set<string>>,
dependents: Map<string, Set<string>>,
- visited: Set<string>
+ _visited: Set<string>
): Promise<void> {
const fullPath = isAbsolutePath(materialPath)
? materialPath
@@ -305,6 +311,61 @@ export class AssetDependencyAnalyzer {
}
}
+ private async collectPrefabRefs(
+ prefabPath: string,
+ prefabUuid: string,
+ dependencies: Map<string, Set<string>>,
+ dependents: Map<string, Set<string>>,
+ visited: Set<string>
+ ): Promise<void> {
+ const fullPath = isAbsolutePath(prefabPath)
+ ? prefabPath
+ : joinPath(this.projectDir_, prefabPath);
+
+ const content = await this.fs_.readFile(fullPath);
+ if (!content) return;
+
+ try {
+ const prefab = JSON.parse(content);
+ const entities = prefab.entities as Array<{
+ components: Array<{ type: string; data: Record<string, unknown> }>;
+ nestedPrefab?: { prefabPath: string };
+ }> | undefined;
+
+ if (!entities) return;
+
+ const scanners = getAllScanners();
+ const scannerMap = new Map<string, AssetRefScanner>();
+ for (const s of scanners) {
+ scannerMap.set(s.componentType, s);
+ }
+
+ for (const entity of entities) {
+ if (entity.nestedPrefab?.prefabPath) {
+ const nestedRef = this.resolveRef(entity.nestedPrefab.prefabPath);
+ const nestedUuid = this.assetDb_.getUuid(nestedRef) ?? nestedRef;
+ this.addEdge(dependencies, dependents, prefabUuid, nestedUuid);
+ await this.collectTransitiveRefs(nestedRef, nestedUuid, dependencies, dependents, visited);
+ }
+
+ for (const comp of entity.components || []) {
+ const scanner = scannerMap.get(comp.type);
+ if (scanner && comp.data) {
+ const refs = scanner.extractRefs(comp.data);
+ for (const ref of refs) {
+ const resolvedRef = this.resolveRef(ref);
+ const refUuid = this.assetDb_.getUuid(resolvedRef) ?? resolvedRef;
+ this.addEdge(dependencies, dependents, prefabUuid, refUuid);
+ await this.collectTransitiveRefs(resolvedRef, refUuid, dependencies, dependents, visited);
+ }
+ }
+ }
+ }
+ } catch {
+ // ignore
+ }
+ }
+
// =========================================================================
// Helpers
// =========================================================================
diff --git a/editor/src/asset/AssetTypes.ts b/editor/src/asset/AssetTypes.ts
index c9f6c868f..3a097eb90 100644
--- a/editor/src/asset/AssetTypes.ts
+++ b/editor/src/asset/AssetTypes.ts
@@ -6,6 +6,7 @@ export const ASSET_EXTENSIONS = new Set([
'atlas', 'skel', 'json',
'esmaterial', 'esshader',
'bmfont', 'fnt',
+ 'esprefab',
]);
export function getAssetType(path: string): string {
@@ -25,6 +26,8 @@ export function getAssetType(path: string): string {
return 'json';
case 'bmfont': case 'fnt':
return 'bitmap-font';
+ case 'esprefab':
+ return 'prefab';
case 'mp3': case 'wav': case 'ogg':
return 'audio';
default:
@@ -32,6 +35,32 @@ export function getAssetType(path: string): string {
}
}
+export type AddressableAssetType =
+ | 'texture' | 'material' | 'spine' | 'bitmap-font'
+ | 'prefab' | 'json' | 'text' | 'binary' | 'audio';
+
+export function toAddressableType(editorType: string): AddressableAssetType | null {
+ switch (editorType) {
+ case 'texture':
+ case 'material':
+ case 'prefab':
+ case 'json':
+ case 'text':
+ case 'binary':
+ case 'audio':
+ return editorType;
+ case 'spine-skeleton':
+ return 'spine';
+ case 'bitmap-font':
+ return 'bitmap-font';
+ case 'spine-atlas':
+ case 'shader':
+ return null;
+ default:
+ return 'binary';
+ }
+}
+
export function looksLikeAssetPath(value: unknown): value is string {
if (typeof value !== 'string') return false;
if (!value.includes('/')) return false;