ShoppingController.cs 7.5 KB

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