EventMgr.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { _decorator, Component, director, Node } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('Manager/EventMgr')
  4. export class EventMgr extends Component {
  5. private static _node: Node
  6. protected onLoad(): void {
  7. EventMgr._node = this.node
  8. director.addPersistRootNode(this.node)
  9. }
  10. public static on(type: string, callback: Function, target?: unknown, useCapture?: any): void {
  11. this._node.on(type, callback, target, useCapture)
  12. }
  13. public static once(type: string, callback: Function, target?: unknown, useCapture?: any): void {
  14. this._node.once(type, callback, target, useCapture)
  15. }
  16. public static emit(type: string, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any): void {
  17. this._node.emit(type, arg0, arg1, arg2, arg3, arg4)
  18. }
  19. public static off(type: string, callback?: Function, target?: unknown, useCapture?: any): void {
  20. this._node.off(type, callback, target, useCapture)
  21. }
  22. public static offAll(target: unknown): void {
  23. this._node.targetOff(target)
  24. }
  25. public static has(type: string, callback?: Function, target?: unknown): boolean {
  26. return this._node.hasEventListener(type, callback, target)
  27. }
  28. }