SingletonMonoBase.cs 837 B

12345678910111213141516171819202122232425262728293031
  1. using System.Collections;
  2. using UnityEngine;
  3. namespace GFGGame.Launcher
  4. {
  5. public class SingletonMonoBase <T> : MonoBehaviour where T : SingletonMonoBase<T>
  6. {
  7. private static T _intance;
  8. public static T Instance
  9. {
  10. get
  11. {
  12. if (_intance == null)
  13. {
  14. GameObject gObj = GameObject.Find("SingletonObject");
  15. if(gObj == null)
  16. {
  17. gObj = new GameObject("SingletonObject");
  18. }
  19. _intance = gObj.GetComponent<T>();
  20. if(_intance == null)
  21. {
  22. _intance = gObj.AddComponent<T>();
  23. }
  24. }
  25. return _intance;
  26. }
  27. }
  28. }
  29. }