import { _decorator, Component } from 'cc'; const { ccclass, property } = _decorator; @ccclass('SingletonBase') export abstract class SingletonBase extends Component { private static _instance: any = null; public static getInstance(this: new () => T): T { if ((this as any)._instance === null) { return null } return (this as any)._instance; } protected onLoad(): void { if ((this.constructor as any)._instance) { this.node.destroy(); return; } (this.constructor as any)._instance = this; } protected onDestroy(): void { if ((this.constructor as any)._instance === this) { (this.constructor as any)._instance = null; } } }