UIHelper.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Button, Color, Component, Node, UIOpacity, UIRenderer, find } from "cc";
  2. export function addBtnClick(btn: Button | Node, onClick: Function, target?) {
  3. if (btn instanceof Node) {
  4. btn.on(Node.EventType.TOUCH_END, onClick, target);
  5. return;
  6. }
  7. if (btn instanceof Button) {
  8. btn.node.on(Button.EventType.CLICK, onClick, target);
  9. return;
  10. }
  11. }
  12. export function removeBtnClick(btn: Button | Node, onClick, target) {
  13. if (btn instanceof Node) {
  14. btn.off(Node.EventType.TOUCH_END, onClick, target);
  15. return;
  16. }
  17. if (btn instanceof Button) {
  18. btn.node.off(Button.EventType.CLICK, onClick, target);
  19. return;
  20. }
  21. }
  22. /**
  23. * 设置透明度
  24. * @param ui
  25. * @param alpha 0 ~ 1;
  26. * @returns
  27. */
  28. export function setAlpha(ui: UIRenderer | Node, alpha: number) {
  29. if (ui instanceof Node) {
  30. const comp = ui.getComponent(UIOpacity) || ui.addComponent(UIOpacity);
  31. comp.opacity = alpha;
  32. return;
  33. }
  34. let color = ui.color.clone();
  35. color.a = 255 * alpha;
  36. ui.color = color;
  37. }