mergemilitary/Assets/MMOModule/Scripts/MMOCore/MMOSingleTon.cs

48 lines
1.1 KiB
C#
Raw Normal View History

2022-09-09 14:35:49 +00:00
using UnityEngine;
namespace MMO
{
public abstract class MMOSingleton<T> : MonoBehaviour where T : MMOSingleton<T>
{
private static T _instance = null;
public static T Instance
{
get
{
if (_instance == null)
{
GameObject go = new GameObject();
DontDestroyOnLoad(go);
go.name = "MMOSingleton:" + typeof(T).ToString();
go.transform.localPosition = Vector3.zero;
go.transform.localEulerAngles = Vector3.zero;
go.transform.localScale = Vector3.one;
_instance = go.AddComponent<T>();
}
return _instance;
}
}
private void Awake()
{
Initialize();
}
protected virtual void Initialize()
{
}
private void OnDestroy()
{
_instance = null;
Dispose();
}
protected virtual void Dispose()
{
}
}
}