与上一篇单例的设计相同,只是需要生成GameObject然后把脚本挂上去。
public class SingletonMonoBehaviour<T> : MonoBehaviour where T : SingletonMonoBehaviour<T>
{
private static T m_instance;
private static readonly object m_lock = new object();
public static bool m_appExit;
virtual protected void Awake()
{
if(m_instance == null)
m_instance = (T)this;
GameObject.DontDestroyOnLoad(gameObject);
Debug.Assert(m_instance == this, string.Format("{0} is duplicated! It is a singleton!", typeof(T).Name));
}
public static T Instance
{
get{
if(m_appExit)
return null;
if(m_instance != null)
return m_instance;
lock(m_lock)
{
if(m_instance != null)
return m_instance;
m_instance = SingletonGameobject.Instance.GetComponent<T> ();
if(m_instance == null)
{
m_instance = SingletonGameobject.Instance.AddComponent<T>();
}
}
return m_instance;
}
}
private void OnDestroy()
{
m_appExit = true;
#if UNITY_EDITOR
Debug.LogWarning(string.Format("SingletonMonobehaviour destroyed, {0}", typeof(T).Name));
#endif
}
}
public class SingletonMonoBehaviour<T, I> : MonoBehaviour where T : SingletonMonoBehaviour<T, I>, I
{
private static I m_instance;
private static readonly object m_lock = new object();
public static bool m_appExit;
virtual protected void Awake()
{
if (m_instance == null)
m_instance = (I)((T)this);
GameObject.DontDestroyOnLoad(gameObject);
Debug.Assert((T)m_instance == ((T)this), string.Format("{0} is duplicated! It is a singleton!", typeof(T).Name));
}
public static I Instance
{
get{
if (m_appExit)
return default(I);
if(m_instance != null)
return m_instance;
lock(m_lock)
{
if(m_instance != null)
return m_instance;
m_instance = (I)(SingletonGameobject.Instance.GetComponent<T>());
if(m_instance == null)
{
m_instance = SingletonGameobject.Instance.AddComponent<T>();
}
}
return m_instance;
}
}
private static void GetSingletonGO()
{
}
private void OnDestroy()
{
m_appExit = true;
#if UNITY_EDITOR
Debug.LogWarning(string.Format("SingletonMonobehaviour destroyed, {0}", typeof(T).Name));
#endif
}
}
class SingletonGameobject
{
private static GameObject m_instance;
private static readonly object m_lock = new object();
public static GameObject Instance
{
get{
if(m_instance != null)
return m_instance;
lock(m_lock)
{
if(m_instance != null)
return m_instance;
if(m_instance == null)
{
m_instance = new GameObject("SingletonGO");
GameObject.DontDestroyOnLoad(m_instance);
}
}
return m_instance;
}
}
}
网友评论