import { _decorator, EventTouch, Node, Enum, Event, CCInteger, Button, color, EventHandler, Component } from 'cc'; import { aa } from '../aa'; const { ccclass, property } = _decorator; @ccclass('BaseButton') export class BaseButton extends Component { private isOK = true protected onEnable(): void { this.init() } list: ButtonCallbackInfo[] = []; init() { let btn = this.getComponent(Button) || this.addComponent(Button); if (!btn) return; btn.transition = 1; // btn.normalColor = color(255, 255, 255, 255); btn.pressedColor = color(221, 221, 221, 255); btn.hoverColor = color(211, 211, 211, 255); btn.disabledColor = color(124, 124, 124, 255); } // 不支持关闭对应方法 on(type, callback: Function, target?, daly = 0.5, useCapture?) { // 这里要用动态代理 let _callback = (ev) => { // aa.audio.getInstance().playOneShot('audios/clickUI') if (this.isOK) { this.isOK = false; this.scheduleOnce( ()=> { this.isOK = true; }, daly); callback.call(target,ev) } } this.list.push(new ButtonCallbackInfo(callback, _callback)) this.node.on(type, _callback, target, useCapture); } off(type, callback: Function, target?, useCapture?) { let _callback = undefined for (let i = this.list.length - 1; i >= 0; i--) { let buttonCallbackInfo = this.list[i] if (buttonCallbackInfo.keyInfo = callback) { _callback = buttonCallbackInfo.vlaueInfo; this.list.splice(i, 1); break; } } this.node.off(type, _callback, target, useCapture); } } class ButtonCallbackInfo { keyInfo: Function; vlaueInfo: Function constructor(key: Function, value: Function) { this.keyInfo = key this.vlaueInfo = value } }