#if UNITY_5_3_OR_NEWER using System.Diagnostics; using UnityEngine; using Debug = UnityEngine.Debug; namespace RocketUtils { public abstract class SingletonBehaviour : MonoBehaviour where T : MonoBehaviour { [SerializeField] protected bool DisableDebugLabels = false; private static DebugInfoBehaviour _debugInfoBehaviour; private static T _instance; private void Awake() { if (Instance == null) { Instance = (T)FindObjectOfType(typeof(T)); if (Instance == null) { Debug.LogWarning("An instance of " + typeof(T) + " is needed in the scene, but there is none."); } else { OnAwake(); } } else if (Instance != this) { Destroy(gameObject); // On reload, singleton already set, so destroy duplicate. } #if ROC_DEBUG_MODE || ROC_LOG_MODE if (!_debugInfoBehaviour && !FindObjectOfType(typeof(DebugInfoBehaviour))) { _debugInfoBehaviour = new GameObject(nameof(DebugInfoBehaviour)) .AddComponent(); DebugInfoBehaviour.DisableDebugLabels = DisableDebugLabels; } #endif } protected abstract void OnAwake(); public static bool IsInstantiated => _instance; public static T Instance { get { if (!_instance) { _instance = (T)FindObjectOfType(typeof(T)); if (!_instance) { Debug.LogWarning("An instance of " + typeof(T) + " is needed in the scene, but there is none."); } else { SingletonBehaviour instance = _instance as SingletonBehaviour; instance.OnAwake(); } } return _instance; } set { _instance = value; } } #if ROC_DEBUG_MODE || ROC_LOG_MODE protected bool DrawDebugLabel { get => DebugInfoBehaviour.DrawDebugLabel; set => DebugInfoBehaviour.DrawDebugLabel = value; } protected bool DrawTimeLabel { get => DebugInfoBehaviour.DrawTimeLabel; set => DebugInfoBehaviour.DrawTimeLabel = value; } protected bool DrawFPSLabel { get => DebugInfoBehaviour.DrawFPSLabel; set => DebugInfoBehaviour.DrawFPSLabel = value; } #endif } } #endif