mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
Fix a Sorting2D bug that priority was not updated in Sorting2D onEnable/onDisable and fix the default priority. (#18650)
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user