UEBase.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import EventMng from "../../manager/EventMng";
  2. import { Constructor } from "./Fundation";
  3. declare global {
  4. type UEClass<T> = Constructor<T> & {
  5. readonly BundleKey: string;
  6. readonly PrefabUrl: string;
  7. readonly CLS: string;
  8. // OnCollectResStatic?: (resCollector: IResCollector) => void;
  9. }
  10. }
  11. const { ccclass, property } = cc._decorator;
  12. @ccclass
  13. export default abstract class UEBase extends cc.Component {
  14. /**通知事件列表 */
  15. private _notifyEventList: Map<string, Function> = new Map<string, Function>();
  16. registerCount: number = 0
  17. /**onLoad 会在组件被首次加载的时候被回调。且优先于任何start */
  18. onLoad() {
  19. }
  20. onDestroy() {
  21. try {
  22. let self = this;
  23. this._notifyEventList.forEach((f, key) => {
  24. // console.log("_notifyEventList key = "+key)
  25. EventMng.off(key, f, self);
  26. }, this);
  27. this._notifyEventList.clear();
  28. } catch (error) {
  29. console.error("onDisable->" + error)
  30. }
  31. if (this.registerCount > 0) {
  32. console.error(this.name + " 按钮注册后没off掉啊喂 \n(╯-_-)╯╧╧ ")
  33. }
  34. }
  35. /**注册notice事件,disable的时候会自动移除 */
  36. initEvent(eventName: string, cb: Function) {
  37. EventMng.on(eventName, cb, this);
  38. this._notifyEventList.set(eventName, cb);
  39. }
  40. }