是这样的,基类:
export default class Singleton {
private static _instance: any = null;
static GetInstance<T>(): T {
if (this._instance === null) {
this._instance = new this();
}
return this._instance;
}
protected constructor() {}
}
继承
@ccclass('SnakeDataManager')
export class SnakeDataManager extends Singleton {
static get Instance() {
return super.GetInstance<SnakeDataManager>();
}
hello(){
console.log("你好");
}
}
调用
SnakeDataManager.Instance.hello();
刚好上周我也纠结这个扩号,之前找了第一种,函数式创建类单例,觉得代码有点多余,偶然在一个帧同步项目发现别人写的,这应该是比较少的代码行数了。