From 9ef56add32b507aa7230a2fd0bedc9142fd01d37 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 23 Apr 2025 20:02:55 +0800 Subject: [PATCH] Fix a Sorting2D bug that priority was not updated in Sorting2D onEnable/onDisable and fix the default priority. (#18650) --- cc.config.json | 1 + cocos/2d/framework/ui-renderer.ts | 7 ++++++- cocos/sorting/sorting-2d.ts | 24 ++++++++++++++++++------ cocos/sorting/sorting-layers.ts | 4 ++++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/cc.config.json b/cc.config.json index b336f5e806..748e013f6f 100644 --- a/cc.config.json +++ b/cc.config.json @@ -832,6 +832,7 @@ "treeShake": { "noSideEffectFiles": [ + "cocos/sorting/sorting-layers.ts", "cocos/xr/event/xr-event-handle.ts", "cocos/xr/xr-enums.ts", "cocos/serialization/instantiate-jit.ts", diff --git a/cocos/2d/framework/ui-renderer.ts b/cocos/2d/framework/ui-renderer.ts index a367503aa6..7c1797232a 100644 --- a/cocos/2d/framework/ui-renderer.ts +++ b/cocos/2d/framework/ui-renderer.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import { DEBUG, EDITOR, JSB } from 'internal:constants'; +import { DEBUG, EDITOR, JSB, USE_SORTING_2D } from 'internal:constants'; import { ccclass, executeInEditMode, requireComponent, tooltip, type, displayOrder, serializable, override, visible, displayName, disallowAnimation, @@ -44,6 +44,7 @@ import { RenderEntity, RenderEntityType, RenderEntityFillColorType } from '../re import { uiRendererManager } from './ui-renderer-manager'; import { RenderDrawInfoType } from '../renderer/render-draw-info'; import { director } from '../../game'; +import { SortingLayers } from '../../sorting/sorting-layers'; import type { Batcher2D } from '../renderer/batcher-2d'; // hack @@ -139,6 +140,10 @@ export class UIRenderer extends Renderer { constructor () { super(); this._renderEntity = this.createRenderEntity(); + + if (USE_SORTING_2D) { + this.priority = SortingLayers.getDefaultPriority(); + } } @override diff --git a/cocos/sorting/sorting-2d.ts b/cocos/sorting/sorting-2d.ts index b7fe532569..52db1b5f38 100644 --- a/cocos/sorting/sorting-2d.ts +++ b/cocos/sorting/sorting-2d.ts @@ -49,6 +49,8 @@ let sorting2DCount = 0; @executeInEditMode @requireComponent(UIRenderer) export class Sorting2D extends Component { + private _isSorting2DEnabled = false; + constructor () { super(); } @@ -94,22 +96,32 @@ export class Sorting2D extends Component { if (!this._uiRenderer) { warnID(16300, this.node.name); } - this._updateSortingPriority(); + } + protected override onEnable (): void { + this._isSorting2DEnabled = true; + this._updateSortingPriority(); ++sorting2DCount; _setSorting2DCount(sorting2DCount); } - protected override onDestroy (): void { + protected override onDisable (): void { + this._isSorting2DEnabled = false; + this._updateSortingPriority(); --sorting2DCount; _setSorting2DCount(sorting2DCount); } protected _updateSortingPriority (): void { - const sortingLayerValue = SortingLayers.getLayerIndex(this._sortingLayer); - const sortingPriority = SortingLayers.getSortingPriority(sortingLayerValue, this._sortingOrder); - if (this._uiRenderer) { - this._uiRenderer.priority = sortingPriority; + const uiRenderer = this._uiRenderer; + if (uiRenderer && uiRenderer.isValid) { + if (this._isSorting2DEnabled) { + const sortingLayerValue = SortingLayers.getLayerIndex(this._sortingLayer); + const sortingPriority = SortingLayers.getSortingPriority(sortingLayerValue, this._sortingOrder); + uiRenderer.priority = sortingPriority; + } else { + uiRenderer.priority = SortingLayers.getDefaultPriority(); + } } } } diff --git a/cocos/sorting/sorting-layers.ts b/cocos/sorting/sorting-layers.ts index 50f9a2ff4a..78bcc2f32a 100644 --- a/cocos/sorting/sorting-layers.ts +++ b/cocos/sorting/sorting-layers.ts @@ -65,6 +65,10 @@ export class SortingLayers { return (((layer + (1 << 15)) << 16) | (order + (1 << 15))) >>> 0; } + public static getDefaultPriority (): number { + return this.getSortingPriority(SortingLayer.default, 0); + } + /** * @zh 获取 Layer 顺序索引 * @en Get Layer index by id