1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- // 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<T>() {
- class Singleton {
- private static minst: any = null;
- public static get Inst(): T {
- if ((<any>this).minst == null) {
- (<any>this).minst = new (<any>this)()
- (<any>this).init()
- }
- return (<any>this).minst
- }
- constructor() {
- }
- public disp() {
- (<any>this).minst = null
- }
- }
- return Singleton
- }
|