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: playable build embedded asset loading and packaging

esenginebc75687f416a
diff --git a/editor/src/builder/PlayableEmitter.ts b/editor/src/builder/PlayableEmitter.ts
index 8c6630f26..7c6a7b40d 100644
--- a/editor/src/builder/PlayableEmitter.ts
+++ b/editor/src/builder/PlayableEmitter.ts
@@ -289,7 +289,13 @@ ${imports}
const assets = new Map<string, string>();
const compiledMaterialPaths = new Set(artifact.compiledMaterials.map(m => m.relativePath));
- for (const relativePath of artifact.assetPaths) {
+ const allFiles = new Set(artifact.assetPaths);
+ const assetsDir = joinPath(projectDir, 'assets');
+ if (await fs.exists(assetsDir)) {
+ await this.walkDirectory(fs, assetsDir, 'assets', allFiles);
+ }
+
+ for (const relativePath of allFiles) {
if (artifact.packedPaths.has(relativePath)) continue;
if (relativePath.endsWith('.esshader')) continue;
@@ -317,6 +323,24 @@ ${imports}
return assets;
}
+ private async walkDirectory(
+ fs: NativeFS,
+ absolutePath: string,
+ relativePath: string,
+ result: Set<string>
+ ): Promise<void> {
+ const entries = await fs.listDirectoryDetailed(absolutePath);
+ for (const entry of entries) {
+ const childAbsolute = joinPath(absolutePath, entry.name);
+ const childRelative = `${relativePath}/${entry.name}`;
+ if (entry.isDirectory) {
+ await this.walkDirectory(fs, childAbsolute, childRelative, result);
+ } else {
+ result.add(childRelative);
+ }
+ }
+ }
+
private assembleHTML(
wasmSdk: string, gameCode: string, sceneData: string,
assets: Map<string, string>,
diff --git a/editor/src/builder/TextureAtlas.ts b/editor/src/builder/TextureAtlas.ts
index 0bdb41d36..45972faae 100644
--- a/editor/src/builder/TextureAtlas.ts
+++ b/editor/src/builder/TextureAtlas.ts
@@ -378,6 +378,8 @@ export class TextureAtlasPacker {
}
}
+ await this.findAtlasFiles(joinPath(this.projectDir_, 'assets'), 'assets', atlasPaths);
+
const result = new Set<string>();
for (const atlasRelPath of atlasPaths) {
const fullPath = joinPath(this.projectDir_, atlasRelPath);
@@ -397,6 +399,28 @@ export class TextureAtlasPacker {
return result;
}
+ private async findAtlasFiles(
+ absolutePath: string,
+ relativePath: string,
+ result: Set<string>
+ ): Promise<void> {
+ let entries: Array<{ name: string; isDirectory: boolean }>;
+ try {
+ entries = await this.fs_.listDirectoryDetailed(absolutePath);
+ } catch {
+ return;
+ }
+ for (const entry of entries) {
+ const childAbsolute = joinPath(absolutePath, entry.name);
+ const childRelative = `${relativePath}/${entry.name}`;
+ if (entry.isDirectory) {
+ await this.findAtlasFiles(childAbsolute, childRelative, result);
+ } else if (/\.atlas$/i.test(entry.name)) {
+ result.add(childRelative);
+ }
+ }
+ }
+
private collectNineSliceTextures(
sceneDataList: Array<{ name: string; data: Record<string, unknown> }>
): Set<string> {
diff --git a/sdk/src/asset/AssetServer.ts b/sdk/src/asset/AssetServer.ts
index fac8acafe..907603bcb 100644
--- a/sdk/src/asset/AssetServer.ts
+++ b/sdk/src/asset/AssetServer.ts
@@ -105,6 +105,7 @@ export class AssetServer {
private fontCache_ = new AsyncCache<FontHandle>();
private prefabCache_ = new AsyncCache<PrefabData>();
private embedded_ = new Map<string, string>();
+ private embeddedOnly_ = false;
private addressableManifest_: AddressableManifest | null = null;
private addressIndex_ = new Map<string, AddressableManifestAsset>();
private labelIndex_ = new Map<string, AddressableManifestAsset[]>();
@@ -130,6 +131,7 @@ export class AssetServer {
for (const [key, value] of Object.entries(assets)) {
this.embedded_.set(key, value);
}
+ this.embeddedOnly_ = typeof location !== 'undefined' && location.protocol === 'file:';
}
// =========================================================================
@@ -601,6 +603,9 @@ export class AssetServer {
private async loadImage(source: string): Promise<HTMLImageElement | ImageBitmap> {
const localPath = this.isLocalPath(source) ? this.toLocalPath(source) : source;
const embedded = this.embedded_.get(localPath);
+ if (this.embeddedOnly_ && !embedded) {
+ throw new Error(`Asset not embedded: ${source}`);
+ }
const imgSrc = embedded ?? localPath;
return new Promise((resolve, reject) => {
const img = platformCreateImage();
@@ -729,6 +734,21 @@ export class AssetServer {
private async loadShaderInternal(path: string): Promise<ShaderHandle> {
const localPath = this.isLocalPath(path) ? this.toLocalPath(path) : path;
+
+ const embedded = this.embedded_.get(localPath);
+ if (embedded) {
+ const content = this.decodeDataUrlText(embedded);
+ const { vertex, fragment } = this.parseEsShader(content);
+ if (!vertex || !fragment) {
+ throw new Error(`Invalid shader format: ${path}`);
+ }
+ return Material.createShader(vertex, fragment);
+ }
+
+ if (this.embeddedOnly_) {
+ throw new Error(`Asset not embedded: ${path}`);
+ }
+
const exists = await platformFileExists(localPath);
if (!exists) {
throw new Error(`Shader file not found: ${path}`);
@@ -788,9 +808,15 @@ export class AssetServer {
if (embedded) {
return JSON.parse(this.decodeDataUrlText(embedded));
}
+ if (this.embeddedOnly_) {
+ throw new Error(`Asset not embedded: ${url}`);
+ }
const text = await platformReadTextFile(localPath);
return JSON.parse(text);
}
+ if (this.embeddedOnly_) {
+ throw new Error(`Asset not embedded: ${url}`);
+ }
const response = await platformFetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch JSON: ${url} (${response.status})`);
@@ -805,8 +831,14 @@ export class AssetServer {
if (embedded) {
return this.decodeDataUrlText(embedded);
}
+ if (this.embeddedOnly_) {
+ throw new Error(`Asset not embedded: ${url}`);
+ }
return platformReadTextFile(localPath);
}
+ if (this.embeddedOnly_) {
+ throw new Error(`Asset not embedded: ${url}`);
+ }
const response = await platformFetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch text: ${url} (${response.status})`);
@@ -821,8 +853,14 @@ export class AssetServer {
if (embedded) {
return this.decodeDataUrlBinary(embedded);
}
+ if (this.embeddedOnly_) {
+ throw new Error(`Asset not embedded: ${url}`);
+ }
return platformReadFile(localPath);
}
+ if (this.embeddedOnly_) {
+ throw new Error(`Asset not embedded: ${url}`);
+ }
const response = await platformFetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch binary: ${url} (${response.status})`);