TreasuresSaleUI.cs 3.8 KB

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