58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
|
|
{
|
|
private static T instance = null;
|
|
|
|
private static readonly object locker = new object();
|
|
|
|
private static bool bAppQuitting;
|
|
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
if (bAppQuitting)
|
|
{
|
|
instance = null;
|
|
return instance;
|
|
}
|
|
|
|
lock (locker)
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = FindObjectOfType<T>();
|
|
if (FindObjectsOfType<T>().Length > 1)
|
|
{
|
|
Debug.LogError("²»Ó¦¸Ã´æÔÚ¶à¸öµ¥Àý£¡");
|
|
return instance;
|
|
}
|
|
|
|
if (instance == null)
|
|
{
|
|
var singleton = new GameObject();
|
|
instance = singleton.AddComponent<T>();
|
|
singleton.name = "(singleton)" + typeof(T);
|
|
singleton.hideFlags = HideFlags.None;
|
|
DontDestroyOnLoad(singleton);
|
|
}
|
|
else
|
|
DontDestroyOnLoad(instance.gameObject);
|
|
}
|
|
instance.hideFlags = HideFlags.None;
|
|
return instance;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
bAppQuitting = false;
|
|
}
|
|
|
|
protected virtual void OnDestroy()
|
|
{
|
|
bAppQuitting = true;
|
|
}
|
|
} |