You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
959 B

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
  5. {
  6. public static T Instance { get; private set; }
  7. [Header("Singleton Settings")]
  8. public bool SetSingletonOnAwake;
  9. public bool isPersistant;
  10. protected virtual void Awake()
  11. {
  12. if (SetSingletonOnAwake)
  13. SetAsSingleton();
  14. }
  15. private void SetAsSingleton()
  16. {
  17. if (Instance != null && Instance != this)
  18. Destroy(this);
  19. else
  20. Instance = (T)this;
  21. if (isPersistant)
  22. DontDestroyOnLoad(transform.root);
  23. }
  24. }
  25. public class Singleton<T> where T : Singleton<T>, new()
  26. {
  27. private static T instance;
  28. public static T Instance
  29. {
  30. get
  31. {
  32. if (instance == null)
  33. instance = new T();
  34. return instance;
  35. }
  36. private set { }
  37. }
  38. }