1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  | 
/**
 * 单例 cc.Component
 */
export class CcSingleton<T> extends cc.Component {
    private static _ins = null;
    public static Ins<T>(c: { new (): T }): T {
        if (this._ins == null) {
            this._ins = cc.director.getScene().getComponentInChildren(c.name);
            if (!this._ins) {
                const _n = new cc.Node('Singleton of ' + c.name);
                _n.parent = cc.director.getScene();
                _n.addComponent(this);
                this._ins = _n.getComponent(c.name);
            }
        }
        return this._ins;
    }
    public init() {
        console.dir(this);
    }
}
  |