TopBarView.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using JCUnityLib;
  6. /* 主界面右上方信息栏 */
  7. public class TopBarView : JCUnityLib.ViewBase
  8. {
  9. [SerializeField] Text[] itemValueTexts;
  10. [SerializeField] Button[] itemPlusButtons;
  11. int[] itemValues = {-1, -1, -1};
  12. public static TopBarView ins;
  13. public void Awake()
  14. {
  15. ins = this;
  16. }
  17. public void OnDestroy()
  18. {
  19. if (ins == this) ins = null;
  20. }
  21. void Start()
  22. {
  23. ResetSiblingIndex();
  24. if (CommonConfig.needToExamine) {
  25. Transform tf = transform.Find("TopBar");
  26. for (int i = 0; i < tf.childCount; i++) {
  27. if (tf.GetChild(i).name.StartsWith("Item")) {
  28. tf.GetChild(i).gameObject.SetActive(false);
  29. }
  30. }
  31. }
  32. // if (Application.platform == RuntimePlatform.IPhonePlayer) {
  33. Transform diamond = transform.Find("TopBar/Item");
  34. diamond.localPosition += Vector3.right * 87;
  35. transform.Find("TopBar/IconLebo").gameObject.SetActive(false);
  36. // }
  37. CheckNeewShowDiamond();
  38. }
  39. void Update()
  40. {
  41. if (itemValues[0] != LoginMgr.myUserInfo.diamond) {
  42. itemValues[0] = LoginMgr.myUserInfo.diamond;
  43. itemValueTexts[0].text = itemValues[0].ToString();
  44. }
  45. // if (itemValues[1] != LoginMgr.myUserInfo.coin) {
  46. // itemValues[1] = LoginMgr.myUserInfo.coin;
  47. // itemValueTexts[1].text = itemValues[1].ToString();
  48. // }
  49. // if (itemValues[2] != LoginMgr.myUserInfo.integral) {
  50. // itemValues[2] = LoginMgr.myUserInfo.integral;
  51. // itemValueTexts[2].text = itemValues[2].ToString();
  52. // }
  53. }
  54. void CheckNeewShowDiamond()
  55. {
  56. bool hasTarget = false;
  57. foreach (var item in _needShowItLockers)
  58. {
  59. if (typeof(ShopView) == item.GetType())
  60. {
  61. hasTarget = true;
  62. break;
  63. }
  64. }
  65. try {
  66. transform.Find("TopBar/Item").gameObject.SetActive(hasTarget);
  67. } catch (System.Exception) {}
  68. }
  69. private static HashSet<ViewBase> _needShowItLockers = new HashSet<ViewBase>();
  70. public static void NeedShowIt(ViewBase locker) {
  71. _needShowItLockers.Add(locker);
  72. ins?.ResetSiblingIndex();
  73. ins?.CheckNeewShowDiamond();
  74. }
  75. public static void DontNeedShowIt(ViewBase locker) {
  76. _needShowItLockers.Remove(locker);
  77. ins?.ResetSiblingIndex();
  78. ins?.CheckNeewShowDiamond();
  79. }
  80. private void ResetSiblingIndex()
  81. {
  82. //如果作为父节点的ViewMgr已OnDestroy,那再操作子节点就会报错。
  83. if (!ViewMgr.HasInstance()) return;
  84. //该节点重排序
  85. int maxOrder = -1;
  86. foreach (var item in _needShowItLockers) {
  87. int sortOrder = item.transform.GetSiblingIndex();
  88. if (sortOrder > maxOrder) maxOrder = sortOrder;
  89. }
  90. transform.SetSiblingIndex(maxOrder + 1);
  91. }
  92. public void GoToSetup() {
  93. AudioMgr.ins.PlayBtn();
  94. ViewManager2.ShowView(ViewManager2.Path_SettingsView);
  95. }
  96. public void GoToShop()
  97. {
  98. AudioMgr.ins.PlayBtn();
  99. if (ShopView.ins) return;
  100. ViewMgr.Instance.ShowView<ShopView>();
  101. }
  102. public void GoToGuider()
  103. {
  104. AudioMgr.ins.PlayBtn();
  105. NewUserGuiderManager.ins.ReviewNewUserGuide();
  106. }
  107. public void GoToLebo()
  108. {
  109. AudioMgr.ins.PlayBtn();
  110. if (ScreenProjectionView.ins) {
  111. GameObject o = ScreenProjectionView.ins.gameObject;
  112. JCUnityLib.CanvasUtils.PlusSortOrder(gameObject, o, 1);
  113. o.SetActive(true);
  114. } else {
  115. GameObject o = GameObject.Instantiate(SceneResourceManager.Instance.GetPrefab("ScreenProjectionView"));
  116. JCUnityLib.CanvasUtils.PlusSortOrder(gameObject, o, 1);
  117. }
  118. }
  119. }