WarehouseController.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 是用于控制 Warehouse UI菜单的
  8. */
  9. public class WarehouseController : MonoBehaviour
  10. {
  11. // Start is called before the first frame update
  12. // 主页面元素
  13. private Button foodButton, toyButton, otherButton, backButton;
  14. private List<ItemInWarehouse> warehouseItems = 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 OnEable()
  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. backButton = root.Q<Button>("back");
  35. // 绑定事件
  36. backButton.clicked += BackBtnClick;
  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. private void OnDisable()
  52. {
  53. warehouseItems.Clear(); // 因为启动时候调用OnEable数据会被重复加载
  54. }
  55. // Update is called once per frame
  56. //void Update()
  57. //{
  58. //}
  59. //菜单语言设定
  60. void LanguageSetting()
  61. {
  62. // 设置 shopping UI界面里面label和按键的语言显示
  63. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "food", EnviromentSetting.languageCode });
  64. foodButton.text = textValue;
  65. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "toy", EnviromentSetting.languageCode });
  66. toyButton.text = textValue;
  67. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "other", EnviromentSetting.languageCode });
  68. otherButton.text = textValue;
  69. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "back", EnviromentSetting.languageCode });
  70. backButton.text = textValue;
  71. }
  72. //获取所有产品列表,切换商品分类之后,需要重新读取和加载
  73. void GetItemList(string type)
  74. {
  75. // 按照类别获取warehouse
  76. warehouseItems.Clear();
  77. // todo 下面这一端要重写。Warehouse获取清单方式和Shopping UI不同
  78. if(UserProperty.itemStocks.TryGetValue(type, out List < ItemStock > items)){
  79. //List<ItemStock> items = UserProperty.itemStocks[type];
  80. // itemDict获取当前分类下所有item
  81. string itemRawData = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "item", type });
  82. Dictionary<string, object> itemDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemRawData);
  83. foreach (ItemStock item in items)
  84. {
  85. var itemDetail = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDict[item.id].ToString());
  86. string price = itemDetail["price"].ToString();
  87. string picture = itemDetail["picture"].ToString();
  88. var desc = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDetail["description"].ToString());
  89. string description = desc[EnviromentSetting.languageCode].ToString();
  90. ItemInWarehouse searchedItem = new(item.id, description, price, picture);
  91. warehouseItems.Add(searchedItem);
  92. }
  93. }
  94. }
  95. // 将items装载到菜单中,切换商品分类之后,需要重新读取和加载
  96. void InstallItems(string type)
  97. {
  98. var root = GetComponent<UIDocument>().rootVisualElement;
  99. itemListView.Clear();
  100. VisualTreeAsset itemResource = Resources.Load<VisualTreeAsset>("Shopping/Item");
  101. List<ItemInWarehouse> items = new();
  102. foreach (var item in warehouseItems)
  103. {
  104. var itemFrame = new VisualElement();
  105. itemFrame = itemResource.CloneTree();
  106. var description = itemFrame.Q<Label>("description");
  107. description.text = item.description;
  108. // 这里复用 shopping UI 里面的 item.uxml
  109. // UI 里面命名为price,实际是qty
  110. var price = itemFrame.Q<Label>("price");
  111. price.text = item.qty;
  112. var texture = Resources.Load<Texture2D>(item.picture);
  113. var picture = itemFrame.Q<VisualElement>("picture");
  114. picture.style.backgroundImage = new StyleBackground(texture);
  115. itemFrame.RegisterCallback<ClickEvent>(e => ItemClick(e, item.id, item.description));
  116. itemListView.Add(itemFrame);
  117. }
  118. }
  119. // 点击商品后跳出确认窗口
  120. void ItemClick(ClickEvent clickEvent, string itemId, string description=null)
  121. {
  122. selectedItemId = itemId;
  123. msgBody.text = description;
  124. msgRoot.visible = true;
  125. }
  126. void BackBtnClick()
  127. {
  128. var uiPlaceholder = GameObject.Find("UI Placeholder");
  129. if (uiPlaceholder != null)
  130. {
  131. var shoppingUI = uiPlaceholder.transform.Find("Warehouse").gameObject;
  132. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  133. shoppingUI.SetActive(false);
  134. vamUI.SetActive(true);
  135. }
  136. }
  137. // 数据读取
  138. void DataLoading()
  139. {
  140. }
  141. void TabSwitch(Button btn)
  142. {
  143. itemListView.style.backgroundColor = btn.resolvedStyle.backgroundColor;
  144. foodButton.transform.scale = new Vector3(1f, 1f, 1);
  145. toyButton.transform.scale = new Vector3(1f, 1f, 1);
  146. otherButton.transform.scale = new Vector3(1f, 1f, 1);
  147. btn.transform.scale = new Vector3(1.2f, 1.2f, 1);
  148. itemListView.Clear();
  149. // 这里btn.name必须和json里面商品类别完全匹配
  150. GetItemList(btn.name);
  151. InstallItems(btn.name);
  152. }
  153. // msg no button 点击
  154. void MsgNoClick()
  155. {
  156. msgRoot.visible = false;
  157. }
  158. // msg yes button 点击
  159. // todo 以后在这里添加调出确认页面,确认后将道具使用请求给服务器
  160. void MsgYesClick()
  161. {
  162. Debug.Log("msg yes clicked");
  163. msgRoot.visible = false;
  164. }
  165. }
  166. public class ItemInWarehouse
  167. {
  168. public string id;
  169. public string description;
  170. public string picture;
  171. public string qty;
  172. public ItemInWarehouse(string id, string desc, string qty, string picture)
  173. {
  174. this.id = id;
  175. this.description = desc;
  176. this.qty = qty;
  177. this.picture = picture;
  178. }
  179. }