ro-webgl/Assets/Src/Core/Base/Singleton.cs
2021-12-21 09:40:39 +08:00

101 lines
2.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//==================================================================================
//==================================================================================
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
using UnityEngine;
/// 单件类非MonoBehaviour类型继承线程安全
/// @单件实例必须在代码中显式创建, GetInstance()不创建实例
/// @bhy
/// @2019.01.03
/// 非MonoBehaviour类型的单件辅助基类利用C#的语法性质简化单件类的定义和使用
/// @T : 单件子类型
public class Singleton<T> where T : class, new()
{
//单件子类实例
private static T s_instance;
protected Singleton()
{
}
//--------------------------------------
/// 创建单件实例
//--------------------------------------
public static void CreateInstance()
{
if (s_instance == null)
{
s_instance = new T();
(s_instance as Singleton<T>).Init();
}
}
//--------------------------------------
/// 删除单件实例
//--------------------------------------
public static void DestroyInstance()
{
if (s_instance != null)
{
(s_instance as Singleton<T>).UnInit();
s_instance = null;
}
}
public static T Instance
{
get
{
if (s_instance == null)
{
CreateInstance();
}
return s_instance;
}
}
//--------------------------------------
/// 返回单件实例
//--------------------------------------
public static T GetInstance()
{
if (s_instance == null)
{
CreateInstance();
}
return s_instance;
}
//--------------------------------------
/// 是否被实例化
//--------------------------------------
public static bool HasInstance()
{
return (s_instance != null);
}
//--------------------------------------
/// 初始化
/// @需要在派生类中实现
//--------------------------------------
public virtual void Init()
{
}
//--------------------------------------
/// 反初始化
/// @需要在派生类中实现
//--------------------------------------
public virtual void UnInit()
{
}
}