WarehouseController.cs 7.8 KB

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