gameObjectZIndexNodeRefresh.ts 987 B

12345678910111213141516171819202122232425262728293031323334
  1. const { ccclass } = cc._decorator;
  2. /**层级更新显示类 */
  3. @ccclass
  4. export default class GameObjectZIndexNodeRefresh extends cc.Component {
  5. /**显示节点组 */
  6. private static nds: cc.Node[] = [];
  7. /**添加显示节点 */
  8. static addNode(nd: cc.Node) {
  9. this.nds.push(nd);
  10. }
  11. /**移除显示节点 */
  12. static removeNode(nd: cc.Node) {
  13. let index = this.nds.indexOf(nd);
  14. if (index >= 0) {
  15. this.nds.splice(index, 1);
  16. }
  17. }
  18. /**更新显示层级 */
  19. update() {
  20. let nds = GameObjectZIndexNodeRefresh.nds;
  21. for (let i = 0; i < nds.length - 1; i++) {
  22. for (let j = i + 1; j < nds.length; j++) {
  23. if (nds[i].y < nds[j].y) {
  24. let temp = nds[i];
  25. nds[i] = nds[j];
  26. nds[j] = temp;
  27. }
  28. }
  29. }
  30. for (let i = 0; i < nds.length; i++) {
  31. nds[i].zIndex = i;
  32. }
  33. }
  34. }