大侠
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

72 lines
2.6 KiB

import { _decorator, Component, director, Node, sp, Sprite, UITransform, Vec3 } from 'cc';
import { PREVIEW } from 'cc/env';
import { features } from 'process';
const { ccclass, property ,menu} = _decorator;
@ccclass('SiblingIndex')
@menu("根据Y轴调整顺序")
export class SiblingIndex extends Component {
//_delayDirtyFrame:number=0;
static _first:boolean = true;
protected update(dt: number): void {
//this._delayDirtyFrame++;
if (!(director.getTotalFrames() % 7)) {
//this._delayDirtyFrame = 0;
let children = this.node.children.concat();
children = children.sort((a, b) => {
return b.position.y - a.position.y;
});
let tempSpine:sp.Skeleton[]=[];
for (let i = 0; i < children.length; i++) {
const tNode = children[i];
let tIndex = tNode.getSiblingIndex()
let targetIndex = i;
let list = tNode.getComponentsInChildren(sp.Skeleton);
if (list && list.length > 0) {
/**如果改节点有一个空数据节点 那么不更新数据 */
if (!this.CheckAllSkeletonDataNoEmpty(list)) {
continue;
}
if (tIndex != targetIndex || SiblingIndex._first) {
for (let j = 0; j < list.length; j++) {
const element = list[j];
//@ts-ignore
element.TempData = element.skeletonData;
//element.skeletonData = null;
}
tNode.setSiblingIndex(targetIndex);
tempSpine = tempSpine.concat(list);
}
} else {
if (tIndex != targetIndex || SiblingIndex._first) {
tNode.setSiblingIndex(targetIndex);
}
}
}
for (let i = 0; i < tempSpine.length; i++) {
const element = tempSpine[i];
//@ts-ignore
element.skeletonData = element.TempData;
}
SiblingIndex._first = false;
}
}
/**检查所有Spine数据不为空 */
CheckAllSkeletonDataNoEmpty(list:sp.Skeleton[]){
return true;
// for (let j = 0; j < list.length; j++) {
// const element = list[j];
// if (!element.skeletonData) {
// return false;
// }
// }
// return true;
}
}
if(PREVIEW){
window["SiblingIndex"] = SiblingIndex
}