Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the problem that there is no cleanup script when direct gc. #1716

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions packages/core/src/ComponentsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ export class ComponentsManager {
script.onStart();
}
}
onStartScripts.length = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this will cause performance problem

}
}

Expand Down Expand Up @@ -182,16 +181,7 @@ export class ComponentsManager {
}

handlingInvalidScripts(): void {
const { _disableScripts: disableScripts } = this;
let length = disableScripts.length;
if (length > 0) {
for (let i = length - 1; i >= 0; i--) {
const disableScript = disableScripts[i];
disableScript._waitHandlingInValid && disableScript._handlingInValid();
}
disableScripts.length = 0;
}

this._handlingDisableScripts();
const { _disposeDestroyScripts: pendingDestroyScripts, _pendingDestroyScripts: disposeDestroyScripts } = this;
this._disposeDestroyScripts = disposeDestroyScripts;
this._pendingDestroyScripts = pendingDestroyScripts;
Expand Down Expand Up @@ -229,10 +219,23 @@ export class ComponentsManager {
this._componentsContainerPool.push(componentContainer);
}

private _handlingDisableScripts() {
const { _disableScripts: disableScripts } = this;
let length = disableScripts.length;
if (length > 0) {
for (let i = length - 1; i >= 0; i--) {
const disableScript = disableScripts[i];
disableScript._waitHandlingInValid && disableScript._handlingInValid();
}
disableScripts.length = 0;
}
}

/**
* @internal
*/
_gc() {
this._handlingDisableScripts();
this._renderers.garbageCollection();
this._onStartScripts.garbageCollection();
this._onUpdateScripts.garbageCollection();
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ export class Engine extends EventDispatcher {
this._spriteRenderDataPool.garbageCollection();
this._spriteMaskRenderDataPool.garbageCollection();
this._textRenderDataPool.garbageCollection();
this._sceneManager._gc();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,15 @@ export class Scene extends EngineObject {
entity._siblingIndex = -1;
}

/**
* @internal
*/
_gc(): void {
this._componentsManager._gc();
this._lightManager._gc();
this.physics._gc();
}

/**
* @internal
*/
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/SceneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ export class SceneManager {
}
}

/**
* @internal
*/
_gc(): void {
this._allCreatedScenes.forEach((scene) => {
scene._gc();
});
}

/**
* @deprecated
* Please use `scenes` instead.
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/Script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ export class Script extends Component {
_handlingInValid(): void {
const componentsManager = this.scene._componentsManager;
const { prototype } = Script;
if (!this._started) {
componentsManager.removeOnStartScript(this);
}
if (this.onUpdate !== prototype.onUpdate) {
componentsManager.removeOnUpdateScript(this);
}
Expand Down
Loading