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 UnityEditor.UIElements;
  6. using UnityEngine;
  7. using UnityEngine.UIElements;
  8. // 这个controller 是用于控制 Shopping UI菜单的
  9. public class ShoppingController : MonoBehaviour
  10. {
  11. // Start is called before the first frame update
  12. // 主页面元素
  13. private Button foodButton, toyButton, otherButton, backButton;
  14. private Label coinQty;
  15. private List<ItemInShop> shoppingItems = new();
  16. private VisualElement itemListView;
  17. //弹窗元素
  18. private Button yesButton, noButton;
  19. private Label msgBody;
  20. private VisualElement msgRoot, msgField;
  21. // 选中的产品
  22. private string selectedItemId;
  23. void Start()
  24. {
  25. // 基层元素获取
  26. var root = GetComponent<UIDocument>().rootVisualElement;
  27. var mainManu = root.Q<VisualElement>("mainManu");
  28. itemListView = root.Q<VisualElement>("itemListView");
  29. foodButton = mainManu.Q<Button>("food");
  30. foodButton.clicked += () => TabSwitch(foodButton);
  31. toyButton = mainManu.Q<Button>("toy");
  32. toyButton.clicked += () => TabSwitch(toyButton);
  33. otherButton = mainManu.Q<Button>("other");
  34. otherButton.clicked += () => TabSwitch(otherButton);
  35. var coinArea = root.Q<VisualElement>("coinArea");
  36. coinQty = coinArea.Q<Label>("coinQty");
  37. backButton = root.Q<Button>("back");
  38. // 弹窗元素获取
  39. msgRoot = root.Q<VisualElement>("msgRoot");
  40. msgField = msgRoot.Q<VisualElement>("msgField");
  41. msgBody = msgField.Q<Label>("msgBody");
  42. yesButton = msgField.Q<Button>("msgYes");
  43. noButton = msgField.Q<Button>("msgNo");
  44. noButton.clicked += MsgNoClick;
  45. //初始化设定
  46. LanguageSetting();
  47. DataLoading();
  48. GetItemList("food");
  49. foodButton.transform.scale = new Vector3(1.2f, 1.2f);
  50. InstallItems("food");
  51. }
  52. // Update is called once per frame
  53. //void Update()
  54. //{
  55. //}
  56. //菜单语言设定
  57. void LanguageSetting()
  58. {
  59. // 设置 shopping UI界面里面label和按键的语言显示
  60. string textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "food", EnviromentSetting.languageCode });
  61. foodButton.text = textValue;
  62. textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "toy", EnviromentSetting.languageCode });
  63. toyButton.text = textValue;
  64. textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "other", EnviromentSetting.languageCode });
  65. otherButton.text = textValue;
  66. textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "back", EnviromentSetting.languageCode });
  67. backButton.text = textValue;
  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. }