Files
test/Assets/PerfectWorld/Scripts/Common/MonoSingleton.cs
2025-10-16 09:10:20 +07:00

38 lines
972 B
C#

using UnityEngine;
namespace BrewMonster
{
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindFirstObjectByType<T>();
if (_instance == null)
{
GameObject obj = new GameObject(typeof(T).Name);
_instance = obj.AddComponent<T>();
}
}
return _instance;
}
}
protected virtual void Awake()
{
_instance = this as T;
Initialize();
}
/// <summary>Override this method to initialize the singleton</summary>
protected virtual void Initialize()
{
}
}
}