TreasuresSaleUI.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using Sirenix.OdinInspector;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public struct Treasure
  7. {
  8. public int id;
  9. public string name;
  10. public Sprite sprite;
  11. public Sprite qualityBackground;
  12. public Sprite qualityBox;
  13. public Color qualityText;
  14. public List<int> tagsId;
  15. public List<Sprite> tags;
  16. public string attribute;
  17. public int price;
  18. public List<string> type;
  19. public List<float> data;
  20. public Treasure(int id, string name, Sprite sprite, Sprite qualityBackground, Sprite qualityBox, Color qualityText, List<int> tagsId, List<Sprite> tags, string attribute, int price, List<string> type, List<float>data)
  21. {
  22. this.id = id;
  23. this.name = name;
  24. this.sprite = sprite;
  25. this.qualityBackground = qualityBackground;
  26. this.qualityBox = qualityBox;
  27. this.qualityText = qualityText;
  28. this.tagsId = new List<int>(tagsId);
  29. this.tags = new List<Sprite>(tags);
  30. this.attribute = attribute;
  31. this.price = price;
  32. this.type = type;
  33. this.data = data;
  34. }
  35. }
  36. public class TreasuresSaleUI : MonoBehaviour
  37. {
  38. [FoldoutGroup("组件")] public ShopUI shopUI;
  39. [FoldoutGroup("组件")] public ButtonUI unlock;
  40. [FoldoutGroup("组件")] public Image iconBackground;
  41. [FoldoutGroup("组件")] public Image icon;
  42. [FoldoutGroup("组件")] public Image[] tags;
  43. [FoldoutGroup("组件")] public TextMeshProUGUI nameText;
  44. [FoldoutGroup("组件")] public TextMeshProUGUI attribute;
  45. [FoldoutGroup("组件")] public ButtonUI buy;
  46. [FoldoutGroup("组件")] public TextMeshProUGUI price;
  47. [FoldoutGroup("组件")] public Image background;
  48. public bool islock;
  49. public bool isNull;
  50. public Treasure treasure;
  51. public TreasureInBag owner;
  52. private void OnEnable()
  53. {
  54. if(owner != null)
  55. {
  56. transform.position = owner.transform.position;
  57. }
  58. }
  59. private void Update()
  60. {
  61. if(owner!=null && gameObject.activeSelf)
  62. {
  63. transform.position = owner.transform.position;
  64. }
  65. }
  66. public void Init()
  67. {
  68. if (unlock != null)
  69. {
  70. ShowLockImg();
  71. }
  72. iconBackground.sprite = treasure.qualityBox;
  73. icon.sprite = treasure.sprite;
  74. int type = treasure.tags.Count % 2;
  75. int len = treasure.tags.Count / 2;
  76. float interval = shopUI.interval;
  77. float left = 0;
  78. switch (type)
  79. {
  80. case 0:
  81. left = -interval / 2 - (len - 1) * interval;
  82. break;
  83. case 1:
  84. left = -len * interval;
  85. break;
  86. }
  87. for (int i = 0; i < 8; i++)
  88. {
  89. Image tag = tags[i];
  90. if (i < treasure.tags.Count)
  91. {
  92. tag.transform.localPosition = new Vector3(left + i * interval, 0, 0);
  93. tag.sprite = treasure.tags[i];
  94. tag.gameObject.SetActive(true);
  95. }
  96. else
  97. {
  98. tag.gameObject.SetActive(false);
  99. }
  100. }
  101. nameText.text = treasure.name;
  102. background.sprite = treasure.qualityBackground;
  103. nameText.color = treasure.qualityText;
  104. attribute.text = treasure.attribute;
  105. if (price != null)
  106. {
  107. price.text = $"{treasure.price}";
  108. gameObject.SetActive(true);
  109. }
  110. isNull = false;
  111. }
  112. public void ChangeLock()
  113. {
  114. islock = !islock;
  115. ShowLockImg();
  116. }
  117. public void ShowLockImg()
  118. {
  119. if (islock)
  120. {
  121. unlock.ChangeImg(1);
  122. }
  123. else
  124. {
  125. unlock.ChangeImg(0);
  126. }
  127. }
  128. public void Buy()
  129. {
  130. GameManager gameManager = GameManager.instance;
  131. if(gameManager.money - treasure.price < 0)
  132. {
  133. return;
  134. }
  135. gameManager.money -= treasure.price;
  136. shopUI.moneyText.text = $"{gameManager.money}";
  137. gameManager.myTreasures.Add(treasure);
  138. for(int i = 0; i < treasure.tagsId.Count; i++)
  139. {
  140. gameManager.myTreasuresTag[treasure.tagsId[i] - 1]++;
  141. shopUI.polygonText[treasure.tagsId[i] - 1].text = $"{gameManager.myTreasuresTag[treasure.tagsId[i] - 1]}";
  142. }
  143. List<int> tagsId = gameManager.myTreasuresTag;
  144. int maxId = tagsId[0];
  145. int id = -1;
  146. if(maxId != 0)
  147. {
  148. id = 0;
  149. }
  150. for (int i = 1; i < 8; i++)
  151. {
  152. if(maxId < tagsId[i])
  153. {
  154. maxId = tagsId[i];
  155. id = i;
  156. }
  157. }
  158. gameManager.maxTreasuresTag = id;
  159. shopUI.DrawUIPolygon();
  160. shopUI.DrawBag();
  161. gameObject.SetActive(false);
  162. isNull = true;
  163. islock = false;
  164. gameManager.GetTreasure(treasure);
  165. }
  166. }