UserBaseUI.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. public abstract class UserBaseUI<T> : MonoBehaviour where T : MonoBehaviour
  3. {
  4. private static T instance;
  5. private static object lockObj = new object();
  6. public static T Instance
  7. {
  8. get
  9. {
  10. if (instance == null)
  11. {
  12. instance = (T)FindObjectOfType(typeof(T));
  13. if (instance == null)
  14. {
  15. lock (lockObj)
  16. {
  17. if (instance == null)
  18. {
  19. GameObject singleton = new GameObject();
  20. instance = singleton.AddComponent<T>();
  21. singleton.name = "(Singleton) " + typeof(T).Name;
  22. DontDestroyOnLoad(singleton);
  23. }
  24. }
  25. }
  26. }
  27. return instance;
  28. }
  29. }
  30. // Start is called before the first frame update
  31. public virtual void Awake()
  32. {
  33. this.transform.parent = GameScene.Instance.transform;
  34. this.transform.localPosition = Vector3.zero;
  35. this.transform.localEulerAngles = Vector3.zero;
  36. this.transform.localScale = Vector3.one;
  37. GameScene.Instance.UIList.Add(Instance.gameObject);
  38. this.gameObject.SetActive(false);
  39. }
  40. public virtual void show()
  41. {
  42. int ct =GameScene.Instance.UIList.Count;
  43. for (int i = 0; i < ct; i++)
  44. {
  45. GameScene.Instance.UIList[i].gameObject.SetActive(false);
  46. }
  47. this.gameObject.SetActive(true);
  48. }
  49. public virtual void hide()
  50. {
  51. }
  52. }