RdSingleton.cs 550 B

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