GameComponent.ts 844 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { _decorator, Component, Node } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. // 游戏关键组件的统一管理
  4. export class GameCompent {
  5. private map = new Map<Function, any>
  6. /**
  7. *
  8. * @param clsz 拿到的类
  9. * @param force 是否强制更新
  10. */
  11. public getCompent<T>(clsz: new () => T, force: boolean = false): T {
  12. let res = this.map.get(clsz)
  13. if (force) {
  14. let obj = new clsz()
  15. this.map.set(clsz, obj)
  16. res = obj
  17. } else {
  18. if (!res) {
  19. let res = new clsz()
  20. this.map.set(clsz, res)
  21. }
  22. }
  23. return res
  24. }
  25. public setCompent(obj: Record<string, any>) {
  26. this.map.set(obj.constructor, obj)
  27. }
  28. public clear() {
  29. this.map.clear()
  30. }
  31. }