Singleton.cs 827 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. namespace XRTool.Util
  3. {
  4. /// <summary>
  5. /// 基本的单例,不依附Unity
  6. /// </summary>
  7. /// <typeparam name="T"></typeparam>
  8. public abstract class Singleton<T> where T :class, new()
  9. {
  10. private static T instance;
  11. private static readonly object syslock = new object();
  12. public static T Instance
  13. {
  14. get
  15. {
  16. //线程安全锁
  17. if (instance == null)
  18. {
  19. lock (syslock)
  20. {
  21. if (instance == null)
  22. {
  23. instance = new T();
  24. }
  25. }
  26. }
  27. return instance;
  28. }
  29. }
  30. }
  31. }