This commit is contained in:
2026-07-14 11:44:07 +08:00
commit 4409c7b031
5906 changed files with 6959792 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
import { _decorator, Component } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('SingletonBase')
export abstract class SingletonBase<T extends Component> extends Component {
private static _instance: any = null;
public static getInstance<T extends Component>(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;
}
}
}