Cocos Creator 源码解读:siblingIndex 与 zIndex

如果只是简单UI就还好,如果是地图和战斗,根本就用不了setSiblingIndex

    /** 根据zIndex统一设置节点顺序 */
    public static setChildrenNodeSortByZIndex(parent: Node): void {
        if (!parent) {
            return;
        }

        let children = parent.children.concat();
        children.sort((a, b): number => {
            if (a.zIndex == null) {
                a.zIndex = 0;
            }
            if (b.zIndex == null) {
                b.zIndex = 0;
            }
            return a.zIndex - b.zIndex;
        });
        let maxIndex = children.length;
        for (const node of children) {
            node.setSiblingIndex(maxIndex);
        }
    }
1赞

感觉大佬分享
3.4.2,我想动态调整同一父节点下子节点的层级,但zIndex,priority好像都去掉了,setSiblingIndex设置没有用,这要怎么处理,有些懵

大佬牛啤 :beers: :beers:

我上面的评论不是可以设置吗?没有zIndex就自己声明一个
不知道怎么声明:直接上代码。

declare module "cc" {
    interface Node {
        /** 借助它实现2.x层级效果,直接赋值不会有任何效果 */
        zIndex: number;
    }
}

image

1赞

好的,感谢感谢,我来试下

MARK!!!

唉,还是以前2dx直接设置zOrder方便好用,现在这个siblingIndex真不方便,而且只能是同级下的排序,如果有特殊情况不在同级,这会很抓狂