ScriptSingleton.cs 581 B

123456789101112131415161718192021222324
  1. namespace Plugins.CxShine.Singleton
  2. {
  3. public abstract class ScriptSingleton<T>
  4. where T : new()
  5. {
  6. private static T _instance;
  7. private static readonly object _lock = new();
  8. public static T Instance
  9. {
  10. get
  11. {
  12. if (_instance == null)
  13. // 上锁,防止重复实例化
  14. lock (_lock)
  15. {
  16. if (_instance == null) _instance = new T();
  17. }
  18. return _instance;
  19. }
  20. }
  21. }
  22. }