ShoppingController.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 OnEnable()
  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. backButton.clicked += BackBtnClick;
  39. // 弹窗元素获取
  40. msgRoot = root.Q<VisualElement>("msgRoot");
  41. msgField = msgRoot.Q<VisualElement>("msgField");
  42. msgBody = msgField.Q<Label>("msgBody");
  43. yesButton = msgField.Q<Button>("msgYes");
  44. noButton = msgField.Q<Button>("msgNo");
  45. noButton.clicked += MsgNoClick;
  46. //初始化设定
  47. LanguageSetting();
  48. DataLoading();
  49. GetItemList("food");
  50. foodButton.transform.scale = new Vector3(1.2f, 1.2f);
  51. InstallItems("food");
  52. }
  53. private void OnDisable()
  54. {
  55. shoppingItems.Clear(); // 因为启动时候调用OnEable数据会被重复加载
  56. }
  57. // Update is called once per frame
  58. //void Update()
  59. //{
  60. //}
  61. //菜单语言设定
  62. void LanguageSetting()
  63. {
  64. // 设置 shopping UI界面里面label和按键的语言显示
  65. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "food", EnviromentSetting.languageCode });
  66. foodButton.text = textValue;
  67. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "toy", EnviromentSetting.languageCode });
  68. toyButton.text = textValue;
  69. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "other", EnviromentSetting.languageCode });
  70. otherButton.text = textValue;
  71. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "back", EnviromentSetting.languageCode });
  72. backButton.text = textValue;
  73. }
  74. /*获取所有产品列表,切换商品分类之后,需要重新读取和加载
  75. */
  76. void GetItemList(string type)
  77. {
  78. // 按照类别获取shopping items
  79. shoppingItems.Clear();
  80. string itemRawData = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "item", type});
  81. Dictionary<string, object> itemDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemRawData);
  82. foreach (string _item in itemDict.Keys)
  83. {
  84. var itemDetail =JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDict[_item].ToString());
  85. string price = itemDetail["price"].ToString();
  86. string picture = itemDetail["picture"].ToString();
  87. var desc = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDetail["description"].ToString());
  88. string description = desc[EnviromentSetting.languageCode].ToString();
  89. ItemInShop SearchedItem = new(_item, description, price, picture);
  90. shoppingItems.Add(SearchedItem);
  91. }
  92. }
  93. // 将items装载到菜单中,切换商品分类之后,需要重新读取和加载
  94. void InstallItems(string type)
  95. {
  96. var root = GetComponent<UIDocument>().rootVisualElement;
  97. itemListView.Clear();
  98. VisualTreeAsset itemResource = Resources.Load<VisualTreeAsset>("Shopping/Item");
  99. List<ItemInShop> items = new();
  100. foreach(var item in shoppingItems)
  101. {
  102. var itemFrame = new VisualElement();
  103. itemFrame = itemResource.CloneTree();
  104. var description = itemFrame.Q<Label>("description");
  105. description.text = item.description;
  106. var price = itemFrame.Q<Label>("price");
  107. price.text = item.price;
  108. var texture = Resources.Load<Texture2D>(item.picture);
  109. var picture = itemFrame.Q<VisualElement>("picture");
  110. picture.style.backgroundImage = new StyleBackground(texture);
  111. itemFrame.RegisterCallback<ClickEvent>(e => ItemClick(e, item.id, item.description));
  112. itemListView.Add(itemFrame);
  113. }
  114. }
  115. // 点击商品后跳出确认窗口
  116. void ItemClick(ClickEvent clickEvent, string itemId, string description=null)
  117. {
  118. selectedItemId = itemId;
  119. msgBody.text = description;
  120. msgRoot.visible = true;
  121. }
  122. // 数据读取
  123. void DataLoading()
  124. {
  125. // 游戏金币数量读取
  126. coinQty.text = UserProperty.coin.ToString();
  127. }
  128. void TabSwitch(Button btn)
  129. {
  130. itemListView.style.backgroundColor = btn.resolvedStyle.backgroundColor;
  131. foodButton.transform.scale = new Vector3(1f, 1f, 1);
  132. toyButton.transform.scale = new Vector3(1f, 1f, 1);
  133. otherButton.transform.scale = new Vector3(1f, 1f, 1);
  134. btn.transform.scale = new Vector3(1.2f, 1.2f, 1);
  135. itemListView.Clear();
  136. // 这里btn.name必须和json里面商品类别完全匹配
  137. GetItemList(btn.name);
  138. InstallItems(btn.name);
  139. }
  140. // msg no button 点击
  141. void MsgNoClick()
  142. {
  143. msgRoot.visible = false;
  144. }
  145. // msg yes button 点击
  146. // todo 以后在这里添加调出确认页面,确认后发送订单给服务器
  147. void MsgYesClick()
  148. {
  149. Debug.Log("msg yes clicked");
  150. msgRoot.visible = false;
  151. }
  152. void BackBtnClick()
  153. {
  154. var uiPlaceholder = GameObject.Find("UI Placeholder");
  155. if (uiPlaceholder != null)
  156. {
  157. var shoppingUI = uiPlaceholder.transform.Find("ShoppingUI").gameObject;
  158. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  159. shoppingUI.SetActive(false);
  160. vamUI.SetActive(true);
  161. }
  162. }
  163. }
  164. public class ItemInShop
  165. {
  166. public string id;
  167. public string description;
  168. public string picture;
  169. public string price;
  170. public ItemInShop(string id, string desc, string price, string picture) {
  171. this.id = id;
  172. this.description = desc;
  173. this.price = price;
  174. this.picture = picture;
  175. }
  176. }