ShopPanel.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class ShopPanel : PopUpPanel
  6. {
  7. public GameObject itemPrefab;
  8. public RectTransform container;
  9. private List<BuyItemData> dataList;
  10. private const int countPrePage = 10;
  11. private int totalPage;
  12. private int currentPage;
  13. private BuyUtil.ItemType itemType = BuyUtil.ItemType.None;
  14. void Awake()
  15. {
  16. currentPanel = this;
  17. }
  18. void OnDestroy()
  19. {
  20. currentPanel = null;
  21. HaloManager.GetInstance().SyncEquipId();
  22. }
  23. // Use this for initialization
  24. void OnDisable ()
  25. {
  26. // ShowItemList(itemType);
  27. HaloManager.GetInstance().SyncEquipId();
  28. }
  29. public void ShowItemList(BuyUtil.ItemType itemType)
  30. {
  31. if(this.itemType == BuyUtil.ItemType.Halo)
  32. HaloManager.GetInstance().SyncEquipId();
  33. this.itemType = itemType;
  34. if(itemType == BuyUtil.ItemType.Halo)
  35. {
  36. InitHaloData();
  37. }
  38. }
  39. private void InitHaloData()
  40. {
  41. HaloManager.GetInstance().RecordStartEquipId();
  42. HaloManager.GetInstance().RequestDataList(()=>{
  43. List<HaloData> list = HaloManager.GetInstance().GetDataList();
  44. dataList = new List<BuyItemData>();
  45. for(int i=0; i<list.Count; i++)
  46. {
  47. HaloData haloData = list[i];
  48. HaloItemData haloItemData = new HaloItemData(haloData);
  49. dataList.Add(haloItemData);
  50. }
  51. totalPage = dataList.Count/countPrePage;
  52. totalPage = Mathf.Max(1, totalPage);
  53. ShowPage(0);
  54. });
  55. }
  56. public void ShowPage(int page)
  57. {
  58. currentPage = NumberUtil.forceBetween(page, 0, totalPage-1);
  59. int start = page * countPrePage;
  60. for(int i=0; i<countPrePage; i++)
  61. {
  62. BuyItemData data = null;
  63. if(start + i < dataList.Count)
  64. {
  65. data = dataList[i];
  66. }
  67. BuyItem item = null;
  68. if(i < container.transform.childCount)
  69. {
  70. item = container.transform.GetChild(i).GetComponent<BuyItem>();
  71. }
  72. else if(data != null)
  73. {
  74. GameObject itemObj = Instantiate<GameObject>(itemPrefab);
  75. PopUpManager.AddToMainCanvas(itemObj, container);
  76. item = itemObj.GetComponent<BuyItem>();
  77. }
  78. if(data == null && item != null)
  79. {
  80. item.gameObject.SetActive(false);
  81. }
  82. else if(data != null)
  83. {
  84. item.gameObject.SetActive(true);
  85. item.Init(data);
  86. }
  87. }
  88. }
  89. // public void OnValueChange(Vector2 pos)
  90. // {
  91. // Debuger.Log(pos);
  92. // scrollbar.value = 0;
  93. // }
  94. public void UpdateInfo()
  95. {
  96. for(int i=0; i<container.childCount; i++)
  97. {
  98. container.GetChild(i).GetComponent<BuyItem>().UpdatePrice();
  99. }
  100. }
  101. public static ShopPanel currentPanel;
  102. public static ShopPanel Show()
  103. {
  104. if(currentPanel != null)
  105. {
  106. PopUpManager.AddToMainCanvas(currentPanel.gameObject);
  107. return currentPanel;
  108. }
  109. GameObject panelObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/UI/Shop/ShopPanel"));
  110. panelObj.GetComponent<ShopPanel>();
  111. PopUpManager.AddToMainCanvas(panelObj);
  112. return currentPanel;
  113. }
  114. public static void Hide()
  115. {
  116. if(currentPanel != null)
  117. currentPanel.Close();
  118. }
  119. public static void Refresh()
  120. {
  121. if(currentPanel != null)
  122. {
  123. currentPanel.UpdateInfo();
  124. }
  125. }
  126. }