| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import { _decorator, Component, Node } from 'cc';
- const { ccclass, property } = _decorator;
- // 游戏关键组件的统一管理
- export class GameCompent {
- private map = new Map<Function, any>
- /**
- *
- * @param clsz 拿到的类
- * @param force 是否强制更新
- */
- public getCompent<T>(clsz: new () => T, force: boolean = false): T {
- let res = this.map.get(clsz)
- if (force) {
- let obj = new clsz()
- this.map.set(clsz, obj)
- res = obj
- } else {
- if (!res) {
- let res = new clsz()
- this.map.set(clsz, res)
- }
- }
- return res
- }
- public setCompent(obj: Record<string, any>) {
- this.map.set(obj.constructor, obj)
- }
- public clear() {
- this.map.clear()
- }
- }
|