TopBarView.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /* 主界面右上方信息栏 */
  6. public class TopBarView : MonoBehaviour
  7. {
  8. [SerializeField] Text[] itemValueTexts;
  9. [SerializeField] Button[] itemPlusButtons;
  10. int[] itemValues = {-1, -1, -1};
  11. public static TopBarView ins;
  12. public void Awake()
  13. {
  14. ins = this;
  15. }
  16. public void OnDestroy()
  17. {
  18. if (ins == this) ins = null;
  19. }
  20. void Start()
  21. {
  22. CheckNeedShowWhenInit();
  23. if (CommonConfig.needToExamine) {
  24. Transform tf = transform.Find("TopBar");
  25. for (int i = 0; i < tf.childCount; i++) {
  26. if (tf.GetChild(i).name.StartsWith("Item")) {
  27. tf.GetChild(i).gameObject.SetActive(false);
  28. }
  29. }
  30. }
  31. }
  32. void Update()
  33. {
  34. if (itemValues[0] != LoginMgr.myUserInfo.diamond) {
  35. itemValues[0] = LoginMgr.myUserInfo.diamond;
  36. itemValueTexts[0].text = itemValues[0].ToString();
  37. }
  38. // if (itemValues[1] != LoginMgr.myUserInfo.coin) {
  39. // itemValues[1] = LoginMgr.myUserInfo.coin;
  40. // itemValueTexts[1].text = itemValues[1].ToString();
  41. // }
  42. // if (itemValues[2] != LoginMgr.myUserInfo.integral) {
  43. // itemValues[2] = LoginMgr.myUserInfo.integral;
  44. // itemValueTexts[2].text = itemValues[2].ToString();
  45. // }
  46. }
  47. static HashSet<MonoBehaviour> _needShowItLockers = new HashSet<MonoBehaviour>();
  48. public static void NeedShowIt(MonoBehaviour locker) {
  49. _needShowItLockers.Add(locker);
  50. #if UNITY_EDITOR
  51. Debug.LogWarning("NeedShowIt, NowCount=" + _needShowItLockers.Count);
  52. #endif
  53. try {
  54. ins.ResetCanvasSortOrder();
  55. if (!ins.gameObject.activeSelf) ins.gameObject.SetActive(true);
  56. }
  57. catch (System.Exception) {}
  58. }
  59. public static void DontNeedShowIt(MonoBehaviour locker) {
  60. _needShowItLockers.Remove(locker);
  61. #if UNITY_EDITOR
  62. Debug.LogWarning("DontNeedShowIt, NowCount=" + _needShowItLockers.Count);
  63. #endif
  64. try {
  65. ins.ResetCanvasSortOrder();
  66. if (ins.gameObject.activeSelf && _needShowItLockers.Count == 0) ins.gameObject.SetActive(false);
  67. }
  68. catch (System.Exception) {}
  69. }
  70. public void CheckNeedShowWhenInit() {
  71. if (_needShowItLockers.Count > 0) {
  72. ins.ResetCanvasSortOrder();
  73. ins.gameObject.SetActive(true);
  74. } else {
  75. ins.gameObject.SetActive(false);
  76. }
  77. }
  78. void ResetCanvasSortOrder()
  79. {
  80. int maxOrder = 0;
  81. foreach (var item in _needShowItLockers) {
  82. int sortOrder = item.GetComponent<Canvas>().sortingOrder;
  83. if (sortOrder > maxOrder) maxOrder = sortOrder;
  84. }
  85. GetComponent<Canvas>().sortingOrder = maxOrder + 1;
  86. }
  87. public void GoToSetup() {
  88. AudioMgr.ins.PlayBtn();
  89. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/SetUpView1"), Vector3.zero, new Quaternion());
  90. JC.Unity.UI.CanvasUtils.PlusSortOrder(gameObject, o, 1);
  91. }
  92. public void GoToShop()
  93. {
  94. AudioMgr.ins.PlayBtn();
  95. if (ShopView.ins) return;
  96. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/ShopView"));
  97. JC.Unity.UI.CanvasUtils.PlusSortOrder(gameObject, o, 1);
  98. }
  99. }