ShoppingController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using Newtonsoft.Json;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. // 这个controller 是用于控制 Shopping UI菜单的
  8. public class ShoppingController : MonoBehaviour
  9. {
  10. // Start is called before the first frame update
  11. // 主页面元素
  12. private Button foodButton, toyButton, otherButton, backButton;
  13. private Label coinQty;
  14. private List<ItemInShop> shoppingItems = new();
  15. private VisualElement itemListView;
  16. //弹窗元素
  17. private Button yesButton, noButton;
  18. private Label msgBody;
  19. private VisualElement msgRoot, msgField;
  20. // 选中的产品
  21. private string selectedItemId;
  22. void Start()
  23. {
  24. // 基层元素获取
  25. var root = GetComponent<UIDocument>().rootVisualElement;
  26. var mainManu = root.Q<VisualElement>("mainManu");
  27. itemListView = root.Q<VisualElement>("itemListView");
  28. foodButton = mainManu.Q<Button>("food");
  29. foodButton.clicked += () => TabSwitch(foodButton);
  30. toyButton = mainManu.Q<Button>("toy");
  31. toyButton.clicked += () => TabSwitch(toyButton);
  32. otherButton = mainManu.Q<Button>("other");
  33. otherButton.clicked += () => TabSwitch(otherButton);
  34. var coinArea = root.Q<VisualElement>("coinArea");
  35. coinQty = coinArea.Q<Label>("coinQty");
  36. backButton = root.Q<Button>("back");
  37. // 弹窗元素获取
  38. msgRoot = root.Q<VisualElement>("msgRoot");
  39. msgField = msgRoot.Q<VisualElement>("msgField");
  40. msgBody = msgField.Q<Label>("msgBody");
  41. yesButton = msgField.Q<Button>("msgYes");
  42. noButton = msgField.Q<Button>("msgNo");
  43. noButton.clicked += MsgNoClick;
  44. //初始化设定
  45. LanguageSetting();
  46. DataLoading();
  47. GetItemList("food");
  48. foodButton.transform.scale = new Vector3(1.2f, 1.2f);
  49. InstallItems("food");
  50. }
  51. // Update is called once per frame
  52. //void Update()
  53. //{
  54. //}
  55. //菜单语言设定
  56. void LanguageSetting()
  57. {
  58. // 设置 shopping UI界面里面label和按键的语言显示
  59. string textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "food", EnviromentSetting.languageCode });
  60. foodButton.text = textValue;
  61. textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "toy", EnviromentSetting.languageCode });
  62. toyButton.text = textValue;
  63. textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "other", EnviromentSetting.languageCode });
  64. otherButton.text = textValue;
  65. textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "back", EnviromentSetting.languageCode });
  66. backButton.text = textValue;
  67. }
  68. /*获取所有产品列表,切换商品分类之后,需要重新读取和加载
  69. */
  70. void GetItemList(string type)
  71. {
  72. // 按照类别获取shopping items
  73. shoppingItems.Clear();
  74. string itemRawData = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "item", type});
  75. Dictionary<string, object> itemDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemRawData);
  76. foreach (string _item in itemDict.Keys)
  77. {
  78. var itemDetail =JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDict[_item].ToString());
  79. string price = itemDetail["price"].ToString();
  80. string picture = itemDetail["picture"].ToString();
  81. var desc = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDetail["description"].ToString());
  82. string description = desc[EnviromentSetting.languageCode].ToString();
  83. ItemInShop SearchedItem = new(_item, description, price, picture);
  84. shoppingItems.Add(SearchedItem);
  85. }
  86. }
  87. // 将items装载到菜单中,切换商品分类之后,需要重新读取和加载
  88. void InstallItems(string type)
  89. {
  90. var root = GetComponent<UIDocument>().rootVisualElement;
  91. itemListView.Clear();
  92. VisualTreeAsset itemResource = Resources.Load<VisualTreeAsset>("Shopping/Item");
  93. List<ItemInShop> items = new();
  94. foreach(var item in shoppingItems)
  95. {
  96. var itemFrame = new VisualElement();
  97. itemFrame = itemResource.CloneTree();
  98. var description = itemFrame.Q<Label>("description");
  99. description.text = item.description;
  100. var price = itemFrame.Q<Label>("price");
  101. price.text = item.price;
  102. var texture = Resources.Load<Texture2D>(item.picture);
  103. var picture = itemFrame.Q<VisualElement>("picture");
  104. picture.style.backgroundImage = new StyleBackground(texture);
  105. itemFrame.RegisterCallback<ClickEvent>(e => ItemClick(e, item.id, item.description));
  106. itemListView.Add(itemFrame);
  107. }
  108. }
  109. // 点击商品后跳出确认窗口
  110. void ItemClick(ClickEvent clickEvent, string itemId, string description=null)
  111. {
  112. selectedItemId = itemId;
  113. msgBody.text = description;
  114. msgRoot.visible = true;
  115. }
  116. // 数据读取
  117. void DataLoading()
  118. {
  119. // 游戏金币数量读取
  120. coinQty.text = UserProperty.coin.ToString();
  121. }
  122. void TabSwitch(Button btn)
  123. {
  124. itemListView.style.backgroundColor = btn.resolvedStyle.backgroundColor;
  125. foodButton.transform.scale = new Vector3(1f, 1f, 1);
  126. toyButton.transform.scale = new Vector3(1f, 1f, 1);
  127. otherButton.transform.scale = new Vector3(1f, 1f, 1);
  128. btn.transform.scale = new Vector3(1.2f, 1.2f, 1);
  129. itemListView.Clear();
  130. // 这里btn.name必须和json里面商品类别完全匹配
  131. GetItemList(btn.name);
  132. InstallItems(btn.name);
  133. }
  134. // msg no button 点击
  135. void MsgNoClick()
  136. {
  137. msgRoot.visible = false;
  138. }
  139. // msg yes button 点击
  140. // todo 以后在这里添加调出确认页面,确认后发送订单给服务器
  141. void MsgYesClick()
  142. {
  143. Debug.Log("msg yes clicked");
  144. msgRoot.visible = false;
  145. }
  146. }
  147. public class ItemInShop
  148. {
  149. public string id;
  150. public string description;
  151. public string picture;
  152. public string price;
  153. public ItemInShop(string id, string desc, string price, string picture) {
  154. this.id = id;
  155. this.description = desc;
  156. this.price = price;
  157. this.picture = picture;
  158. }
  159. }