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(sdk): particle and mesh2d plugins drop their onDespawn hook on cleanup

esengine110e757ff5d6
diff --git a/sdk/src/mesh2d.ts b/sdk/src/mesh2d.ts
index 6ac5b0a87..dda8fa640 100644
--- a/sdk/src/mesh2d.ts
+++ b/sdk/src/mesh2d.ts
@@ -89,6 +89,7 @@ export const Meshes2D = defineResource<Mesh2DAPI>(null!, 'Meshes2D');
export class Mesh2DPlugin implements Plugin {
name = 'mesh2d';
+ private offDespawn_: (() => void) | null = null;
build(app: App): void {
const module = app.wasmModule as ESEngineModule;
@@ -96,7 +97,7 @@ export class Mesh2DPlugin implements Plugin {
const api = new Mesh2DAPI(module, registry);
app.insertResource(Meshes2D, api);
- app.world.onDespawn((entity: Entity) => {
+ this.offDespawn_ = app.world.onDespawn((entity: Entity) => {
if (api.getGeometry(entity)) api.clearGeometry(entity);
});
@@ -119,6 +120,11 @@ export class Mesh2DPlugin implements Plugin {
},
});
}
+
+ cleanup(): void {
+ this.offDespawn_?.();
+ this.offDespawn_ = null;
+ }
}
export const mesh2dPlugin = new Mesh2DPlugin();
diff --git a/sdk/src/particle/ParticlePlugin.ts b/sdk/src/particle/ParticlePlugin.ts
index d1abfcf53..3d570684f 100644
--- a/sdk/src/particle/ParticlePlugin.ts
+++ b/sdk/src/particle/ParticlePlugin.ts
@@ -18,6 +18,7 @@ export class ParticlePlugin implements Plugin {
private readonly gradients_ = new Map<number, Gradient>();
/** Entity → authored size-over-life curve (out-of-band; baked to the C++ LUT). */
private readonly sizeCurves_ = new Map<number, Curve>();
+ private offDespawn_: (() => void) | null = null;
build(app: App): void {
const module = app.wasmModule as ESEngineModule;
@@ -30,7 +31,7 @@ export class ParticlePlugin implements Plugin {
const gradients = this.gradients_;
const sizeCurves = this.sizeCurves_;
- app.world.onDespawn((entity) => {
+ this.offDespawn_ = app.world.onDespawn((entity) => {
if (gradients.delete(entity)) api.setColorLut(entity, null);
if (sizeCurves.delete(entity)) api.setSizeLut(entity, null);
});
@@ -67,6 +68,13 @@ export class ParticlePlugin implements Plugin {
{ name: 'ParticleSystem' }
), { runIf: fxPreviewOrPlayMode });
}
+
+ cleanup(): void {
+ this.offDespawn_?.();
+ this.offDespawn_ = null;
+ this.gradients_.clear();
+ this.sizeCurves_.clear();
+ }
}
export const particlePlugin = new ParticlePlugin();