Singleton.cs 890 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. namespace Rokid.UXR
  3. {
  4. /// <summary>
  5. /// Singleton
  6. /// </summary>
  7. public abstract class Singleton<T> where T : Singleton<T>
  8. {
  9. private static T m_Instance;
  10. private static object m_Locker = new object();
  11. public static T Instance
  12. {
  13. get
  14. {
  15. if (m_Instance == null)
  16. {
  17. lock (m_Locker)
  18. {
  19. if (m_Instance == null)
  20. {
  21. m_Instance = Activator.CreateInstance<T>();
  22. m_Instance.OnSingletonInit();
  23. }
  24. }
  25. }
  26. return m_Instance;
  27. }
  28. }
  29. protected Singleton() { }
  30. protected virtual void OnSingletonInit()
  31. {
  32. }
  33. }
  34. }