// Learn TypeScript: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html const { ccclass, property } = cc._decorator; @ccclass export default class MyComponent extends cc.Component { _eventMap = []; onLoad() { // super.onLoad(); this.devicesFit(); } //注册事件 regEvent(type, callback, target = null, once = null) { cc.systemEvent.on(type, callback, target, once); this._eventMap.push({ type: type, callback: callback, target: target, once: once }); } regEventOnce(type, callback, target = null) { cc.systemEvent.once(type, callback, target); this._eventMap.push({ type: type, callback: callback, target: target, once: null }); } // 机型适配 子类重载 devicesFit() { } onDestroy() { // super.onDestroy(); for (let i = 0; i < this._eventMap.length; i++) { let data = this._eventMap[i]; cc.systemEvent.off(data.type, data.callback, data.target); } delete this._eventMap; this.unscheduleAllCallbacks(); } } export function Singleton() { class Singleton { private static minst: any = null; public static get Inst(): T { if ((this).minst == null) { (this).minst = new (this)() (this).init() } return (this).minst } constructor() { } public disp() { (this).minst = null } } return Singleton }