public class Player
{
//静态私有变量,存取我们的单一实例
private static Player instance;
//私有构造器,供内部使用
private Player()
{
}
//公开的静态方法供我们调用来获取实例
public static Player GetInstance()
{
//没有这个实例我们就创建
if (instance == null)
{
instance = new Player();
Debug.Log("这是第一次创建实例");
return instance;
}
//有的话直接获取
Debug.Log("实例已经存在,我拿来直接用了");
return instance;
}
}
单例模式 Singleton(单例)
最新推荐文章于 2024-02-26 18:00:54 发布