165 lines
3.5 KiB
C#
165 lines
3.5 KiB
C#
using UnityEngine;
|
|
|
|
namespace Script.Utils
|
|
{
|
|
public abstract class NormalSingleton<T> where T : new()
|
|
{
|
|
private static T mInstance;
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
if (mInstance == null)
|
|
{
|
|
mInstance = new T();
|
|
}
|
|
|
|
return mInstance;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 动态(Dynamic)
|
|
/// </summary>
|
|
public abstract class D_MonoSingleton<T> : MonoBehaviour where T : D_MonoSingleton<T>
|
|
{
|
|
private static T mInstance = null;
|
|
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
if (mInstance == null)
|
|
{
|
|
GameObject tFramewokGo = GameObject.Find("TKFramework");
|
|
if (tFramewokGo == null)
|
|
{
|
|
tFramewokGo = new GameObject("TKFramework");
|
|
DontDestroyOnLoad(tFramewokGo);
|
|
}
|
|
GameObject tGo = new GameObject();
|
|
tGo.transform.SetParent(tFramewokGo.transform);
|
|
tGo.name = typeof(T).ToString();
|
|
tGo.transform.localPosition = Vector3.zero;
|
|
tGo.transform.localEulerAngles = Vector3.zero;
|
|
tGo.transform.localScale = Vector3.one;
|
|
mInstance = tGo.AddComponent<T>();
|
|
}
|
|
return mInstance;
|
|
}
|
|
}
|
|
|
|
protected Transform mTrans;
|
|
|
|
private void Awake()
|
|
{
|
|
mTrans = transform;
|
|
Initialize();
|
|
}
|
|
|
|
protected virtual void Initialize()
|
|
{
|
|
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
mInstance = null;
|
|
|
|
Dispose();
|
|
}
|
|
|
|
protected virtual void Dispose()
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 静态(static)
|
|
/// </summary>
|
|
public abstract class S_MonoSingleton<T> : MonoBehaviour where T : S_MonoSingleton<T>
|
|
{
|
|
private static T mInstance = null;
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
return mInstance;
|
|
}
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (mInstance != null && mInstance != (T)this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
mInstance = (T)this;
|
|
Initialize();
|
|
}
|
|
|
|
protected virtual void Initialize()
|
|
{
|
|
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
mInstance = null;
|
|
|
|
Dispose();
|
|
}
|
|
|
|
protected virtual void Dispose()
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 静态常驻
|
|
/// </summary>
|
|
public abstract class O_MonoSingleton<T> : MonoBehaviour where T : O_MonoSingleton<T>
|
|
{
|
|
private static T mInstance = null;
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
return mInstance;
|
|
}
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (mInstance != null && mInstance != (T)this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
mInstance = (T)this;
|
|
DontDestroyOnLoad(gameObject);
|
|
Initialize();
|
|
}
|
|
|
|
protected virtual void Initialize()
|
|
{
|
|
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
mInstance = null;
|
|
|
|
Dispose();
|
|
}
|
|
|
|
protected virtual void Dispose()
|
|
{
|
|
|
|
}
|
|
}
|
|
} |