using UnityEngine;
namespace Miss
{
//继承自Monobehaviour类单例
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{
private static object m_Lock = new object();
private static T m_Instance;
public static T GetInstance()
{
if (m_Instance == null)
{
//避免多线程时同时调用
lock (m_Lock)
{
if (m_Instance == null)
{
m_Instance = GameObject.FindObjectOfType<T>();
if (m_Instance == null)
{
GameObject go = new GameObject();
m_Instance = go.AddComponent<T>();
go.name = typeof(T).ToString();
DontDestroyOnLoad(go);
}
}
}
}
return m_Instance;
}
private void OnDestroy()
{
Dispose();
}
public void Init()
{
OnInit();
}
private void Dispose()
{
OnDispose();
}
//初始化
protected virtual void OnInit() { }
protected virtual void OnDispose() { }
}
//纯类单例
public class Singleton<T> where T : class, new()
{
private static object m_Lock = new object();
private static T m_Instance;
public static T GetInstance()
{
if (m_Instance == null)
{
//避免多线程时同时调用
lock (m_Lock)
{
if (m_Instance == null)
{
m_Instance = new T();
}
}
}
return m_Instance;
}
public void Init()
{
OnInit();
}
private void Dispose() { }
/// <summary>
/// 数据初始化位置
/// </summary>
protected virtual void OnInit() { }
protected virtual void OnDispose() { }
}
}
系统模块:单例模块
于 2023-10-24 10:02:05 首次发布