using UnityEngine; //----------------------------------------------------------------------------- // Copyright 2014-2020 RenderHeads Ltd. All rights reserved. //----------------------------------------------------------------------------- namespace RenderHeads.Media.AVProDeckLink { public class Singleton : MonoBehaviour where T : MonoBehaviour { private static T _instance; private static object _lock = new object(); private static bool _manuallyCreated = true; public static T Instance { get { if (applicationIsQuitting) { return null; } lock (_lock) { if (_instance == null) { _instance = (T)FindObjectOfType(typeof(T)); if (FindObjectsOfType(typeof(T)).Length > 1) { Debug.LogError("[Singleton] Something went really wrong " + " - there should never be more than 1 singleton!" + " Reopenning the scene might fix it."); return _instance; } if (_instance == null) { _manuallyCreated = false; GameObject singleton = new GameObject(); _instance = singleton.AddComponent(); singleton.name = "(singleton) " + typeof(T).ToString(); Debug.Log("constructing " + singleton.name); DontDestroyOnLoad(singleton); } } return _instance; } } } private static bool applicationIsQuitting = false; public void Awake() { if (_manuallyCreated) { applicationIsQuitting = false; } } public void OnDestroy() { applicationIsQuitting = true; } } }