| 123456789101112131415161718192021 |
- import { _decorator, Component, Node } from 'cc';
- const { ccclass, property } = _decorator;
- type SingletonConstructor<T> = {
- new (...args: any[]): T;
- }
- export class Singleton {
- private static instances: Map<Function, any> = new Map();
- private constructor() {}
- public static getInstance<T>(cls: SingletonConstructor<T>): T {
- if (!this.instances.has(cls)) {
- this.instances.set(cls, new cls());
- }
- return this.instances.get(cls);
- }
- }
|