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