설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentSingleton.cs 1.3KB

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