35 lines
730 B
C#
35 lines
730 B
C#
using UnityEngine;
|
|
|
|
namespace AibisDream.Kit
|
|
{
|
|
public abstract class Singleton<T> : MonoBehaviour, ISingleton where T : Singleton<T>
|
|
{
|
|
public static T Instance { get; private set; }
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
if (Instance != null)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
Instance = (T)this;
|
|
OnSingletonInit();
|
|
}
|
|
|
|
public virtual void OnSingletonInit()
|
|
{
|
|
}
|
|
|
|
public static bool IsInitialized => Instance != null;
|
|
|
|
protected virtual void OnDestroy()
|
|
{
|
|
if (Instance == this)
|
|
{
|
|
Instance = null;
|
|
}
|
|
}
|
|
}
|
|
}
|