[RuntimeInitializeOnLoadMethod]
private static void CheckInstance()
{
if (instance == null && Application.isPlaying)
{
var go = new GameObject(
"CustomComponent", typeof(CustomComponent));
GameObject.DontDestroyOnLoad(go);
instance = go.GetComponent<CustomComponent>();
}
}
[RuntimeInitializeOnLoadMethod]这个特性在Awake()之后执行。
CheckInstance()方法在Awake()方法之后创建了名字为CustomComponent的游戏对象,并把CustomComponent脚本挂到CustomComponent的游戏对象上,CustomComponent脚本已经创建为一个单例