A game about forced loneliness, made by TACStudios
at master 50 lines 1.4 kB view raw
1namespace UnityEngine.Rendering 2{ 3 // Use this class to get a static instance of a component 4 // Mainly used to have a default instance 5 6 /// <summary> 7 /// Singleton of a Component class. 8 /// </summary> 9 /// <typeparam name="TType">Component type.</typeparam> 10 public static class ComponentSingleton<TType> 11 where TType : Component 12 { 13 static TType s_Instance = null; 14 /// <summary> 15 /// Instance of the required component type. 16 /// </summary> 17 public static TType instance 18 { 19 get 20 { 21 if (s_Instance == null) 22 { 23 GameObject go = new GameObject("Default " + typeof(TType).Name) { hideFlags = HideFlags.HideAndDontSave }; 24 25#if !UNITY_EDITOR 26 GameObject.DontDestroyOnLoad(go); 27#endif 28 29 go.SetActive(false); 30 s_Instance = go.AddComponent<TType>(); 31 } 32 33 return s_Instance; 34 } 35 } 36 37 /// <summary> 38 /// Release the component singleton. 39 /// </summary> 40 public static void Release() 41 { 42 if (s_Instance != null) 43 { 44 var go = s_Instance.gameObject; 45 CoreUtils.Destroy(go); 46 s_Instance = null; 47 } 48 } 49 } 50}