Singleton.cs 639 B

12345678910111213141516171819202122232425262728293031
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /*
  5. 配置单例模板
  6. */
  7. public class Singleton<T> where T: new()
  8. {
  9. private static T instance = default(T);
  10. private static object sync = new Object();
  11. public static T Instance
  12. {
  13. get
  14. {
  15. if (instance == null)
  16. {
  17. lock (sync)
  18. {
  19. if (instance == null)
  20. {
  21. instance = new T();
  22. }
  23. }
  24. }
  25. return instance;
  26. }
  27. }
  28. }