const { ccclass } = cc._decorator; /**层级更新显示类 */ @ccclass export default class RoleZIndexRefresh extends cc.Component { /**显示节点组 */ private static nds: cc.Node[] = []; /**添加显示节点 */ static addNode(nd: cc.Node) { this.nds.push(nd); } /**移除显示节点 */ static removeNode(nd: cc.Node) { let index = this.nds.indexOf(nd); if (index >= 0) { this.nds.splice(index, 1); } } /**更新显示层级 */ update() { let nds = RoleZIndexRefresh.nds; for (let i = 0; i < nds.length - 1; i++) { for (let j = i + 1; j < nds.length; j++) { if (nds[i].y < nds[j].y) { let temp = nds[i]; nds[i] = nds[j]; nds[j] = temp; } } } for (let i = 0; i < nds.length; i++) { nds[i].zIndex = i; } } }