import { Button, Color, Component, Node, UIOpacity, UIRenderer, find } from "cc"; export function addBtnClick(btn: Button | Node, onClick: Function, target?) { if (btn instanceof Node) { btn.on(Node.EventType.TOUCH_END, onClick, target); return; } if (btn instanceof Button) { btn.node.on(Button.EventType.CLICK, onClick, target); return; } } export function removeBtnClick(btn: Button | Node, onClick, target) { if (btn instanceof Node) { btn.off(Node.EventType.TOUCH_END, onClick, target); return; } if (btn instanceof Button) { btn.node.off(Button.EventType.CLICK, onClick, target); return; } } /** * 设置透明度 * @param ui * @param alpha 0 ~ 1; * @returns */ export function setAlpha(ui: UIRenderer | Node, alpha: number) { if (ui instanceof Node) { const comp = ui.getComponent(UIOpacity) || ui.addComponent(UIOpacity); comp.opacity = alpha; return; } let color = ui.color.clone(); color.a = 255 * alpha; ui.color = color; }