using UnityEngine; namespace BigSpace.XRCore.Base { public abstract class MonoSingleton : MonoBehaviour where T : MonoSingleton { private static T _instance; private static readonly object _lock = new object(); public static T Instance { get { if (_instance == null) { lock (_lock) { _instance = FindObjectOfType(); if (_instance == null) { GameObject singletonObject = new GameObject(typeof(T).Name); _instance = singletonObject.AddComponent(); DontDestroyOnLoad(singletonObject); } } } return _instance; } } protected virtual void Awake() { if (_instance == null) { _instance = this as T; DontDestroyOnLoad(gameObject); } else if (_instance != this) { Destroy(gameObject); } } protected virtual void OnDestroy() { if (_instance == this) { _instance = null; } } } }