各位大佬
大厅加子游戏模式
大厅和子游戏是不同的 Bundle
子游戏会用大厅的一些类
大厅不会引用子游戏的类
那这个类~~~
是写成单例模式比较好
还是new一个对象挂在window上比较好

我是这样做的:如果这个类是用来存放临时数据,那单例,否则new
从始至终只有一个实例我都写成单例,其它的就new或者instantiate
我有个问题,单例脚本是不是不用挂在节点上也能执行?那不需要继承compoment吧?不继承的话start,onload,update函数也用不了是吗?
不继承compoment,不挂节点上也能运行
是的,不需要,我有不少单例就这么写的。比如网络 Network
Network.js
window.Network = {}
Network.send = function(mess)
{
}
Network.connect = function()
{
}
自己的闭源环境,我更推荐 window.HallUtil = {} 这样。
我一般是这么写单例:
// Singleton.ts
class Singleton {
private __data: number = 0;
constructor() {}
public test() {
this.__data++;
console.log('Singleton test data', this.__data);
}
}
export default new Singleton;
然后这么调用:
// Game.ts
import { _decorator} from 'cc';
import Singleton from './Singleton';
const { ccclass, property } = _decorator;
@ccclass('Game')
export default class Game extends Component {
protected onLoad(): void {
Singleton.test();
}
}
你这好像不是单例吧
哪里不是了,在任何地方导入的Singleton都是同一个对象。