TreasuresSaleUI.cs 4.5 KB

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