public static GameManager Instance{
get{return instance;}
}
private static GameManager instance = null;
void Awake()
{
//检查instance是否已经存在
//如果存在,就销毁掉自己
if(instance)
{
DestroyImmediate(gameObject);
return;
}
//使自己成为唯一的实例
instance = this;
//如果存在多关卡切换,就需要加上下面这句
DontDestroyOnLoad(gameObject);
}
这是我目前最常用的简洁的单态写法,当然也有更多写法,比如自动在场景中生成一个物体,并挂上这个脚本,但是我发现在实践中,越是简单的越好用。