publicabstractclassMonoSingleton<T> : MonoBehaviourwhereT : Component { privatestatic T m_instance = null; publicstatic T Instance { get { GameObject go = GameObject.Find("$MonoSingleton"); if (go == null) { go = new GameObject("$MonoSingleton"); } if (!go.IsDestroyed()) { DontDestroyOnLoad(go); }
if (m_instance == null) { GameObject child = new GameObject(typeof(T).Name); GameObject.DontDestroyOnLoad(child); child.transform.SetParent(go.transform); m_instance = child.AddComponent<T>(); } return m_instance; } }