版本:cocos creator 3.8
节点包括所有子节点置灰的方法:如下:直接上代码了
setGray(target: Node | Sprite | Label, gray: boolean) {
if (target instanceof Node) {
target.walk((child) => {
const spr = child.getComponent(Sprite)
const lab = child.getComponent(Label)
spr && this.setGray(spr, gray)
lab && this.setGray(lab, gray)
})
} else if (target instanceof Sprite) {
target.grayscale = gray;
} else if (target instanceof Label) {
if (gray) {
const mat = builtinResMgr.get<Material>('ui-sprite-gray-material')
target.customMaterial = mat
} else {
target.customMaterial = null;
}
}
}
