using UnityEngine; public abstract class SingletonMonoBehavior : MonoBehaviour where T : SingletonMonoBehavior { private static T _instance; public static T Instance { get { if (_instance == null) { _instance = FindFirstObjectByType(); if (_instance == null) { var go = new GameObject(typeof(T).Name); _instance = go.AddComponent(); } } return _instance; } } protected virtual void Awake() { if (_instance != null && _instance != this) { Destroy(gameObject); return; } _instance = (T)this; DontDestroyOnLoad(gameObject); } }