【Unity-学习-017】 Unity 带 MonoBehaviour 的单例

public abstract class SingletonMonoBehaviour : MonoBehaviour where T : SingletonMonoBehaviour
{
    protected static T sInstance = null;
    protected static bool IsCreate = false;
    public static bool s_debugDestroy = false;

    public static T Instance
    {
        get
        {
            if (s_debugDestroy)
            {
                return null;
            }
            CreateInstance();
            return sInstance;
        }
    }

    protected virtual void Awake()
    {
        if (sInstance == null)
        {
            sInstance = this as T;
            IsCreate = true;

            Init();
        }
    }

    public static bool InstanceIsNull()
    {
        return sInstance == null;
    }

    protected virtual void Init()
    {
    }

    protected virtual void OnDestroy()
    {
        sInstance = null;
        IsCreate = false;
    }

    public static void CreateInstance()
    {
        if (IsCreate == true)
            return;

        IsCreate = true;
        T[] managers = GameObject.FindObjectsOfType(typeof(T)) as T[];
        if (managers.Length != 0)
        {
            if (managers.Length == 1)
            {
                sInstance = managers[0];
                sInstance.gameObject.name = typeof(T).Name;
                DontDestroyOnLoad(sInstance.gameObject);
                return;
            }
            else
            {
                foreach (T manager in managers)
                {
                    Destroy(manager.gameObject);
                }
            }
        }

        GameObject gO = new GameObject(typeof(T).Name, typeof(T));
        sInstance = gO.GetComponent();
        DontDestroyOnLoad(sInstance.gameObject);
    }

    public static void ReleaseInstance()
    {
        if (sInstance != null)
        {
            Destroy(sInstance.gameObject);
            sInstance = null;
            IsCreate = false;
        }
    }

}

 

你可能感兴趣的:(【Unity-学习-017】 Unity 带 MonoBehaviour 的单例)