From 37951a135d01b69ee37fa0e78bbbfabe6084fc65 Mon Sep 17 00:00:00 2001 From: GengineJS <476393671@qq.com> Date: Thu, 8 May 2025 09:58:57 +0800 Subject: [PATCH] Fixed the issue where batch processing would be interrupted when some ui components were displayed or hidden (#18688) --- cocos/2d/framework/ui-renderer.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cocos/2d/framework/ui-renderer.ts b/cocos/2d/framework/ui-renderer.ts index 7c1797232a..82c14fefae 100644 --- a/cocos/2d/framework/ui-renderer.ts +++ b/cocos/2d/framework/ui-renderer.ts @@ -385,13 +385,22 @@ export class UIRenderer extends Renderer { this._markForUpdateRenderData(); } + private _destroyData (): void { + this.destroyRenderData(); + if (this._materials) { + for (let i = 0; i < this._materials.length; i++) { + this.setSharedMaterial(null, i, true); + } + } + } + public onDisable (): void { this.node.off(NodeEventType.ANCHOR_CHANGED, this._nodeStateChange, this); this.node.off(NodeEventType.SIZE_CHANGED, this._nodeStateChange, this); this.node.off(NodeEventType.PARENT_CHANGED, this._colorDirty, this); // When disabling, it is necessary to free up idle space to fully utilize chunks // and avoid breaking batch processing. - this.destroyRenderData(); + this._destroyData(); uiRendererManager.removeRenderer(this); this._renderFlag = false; this._renderEntity.enabled = false; @@ -402,13 +411,7 @@ export class UIRenderer extends Renderer { if (this.node._uiProps.uiComp === this) { this.node._uiProps.uiComp = null; } - this.destroyRenderData(); - if (this._materialInstances) { - for (let i = 0; i < this._materialInstances.length; i++) { - const instance = this._materialInstances[i]; - if (instance) { instance.destroy(); } - } - } + this._destroyData(); } /**