WarehouseController.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. }else if (type == "toy")
  86. {
  87. items = UserProperty.toy;
  88. }
  89. else if (type == "other")
  90. {
  91. items = UserProperty.other;
  92. }
  93. foreach (var item in items)
  94. {
  95. var itemFrame = new VisualElement();
  96. itemFrame = itemResource.CloneTree();
  97. // 从language.json读取item的文字描述
  98. string desc = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", type, item.Key, "description" , EnviromentSetting.languageCode});
  99. var description = itemFrame.Q<Label>("description");
  100. description.text = desc;
  101. // 这里复用 shopping UI 里面的 item.uxml
  102. // UI 里面命名为price,实际是qty
  103. var price = itemFrame.Q<Label>("price");
  104. price.text = item.Value.ToString();
  105. // 读取图片
  106. string picturePath = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", type, item.Key, "picture" });
  107. var texture = Resources.Load<Texture2D>(picturePath);
  108. var picture = itemFrame.Q<VisualElement>("picture");
  109. picture.style.backgroundImage = new StyleBackground(texture);
  110. itemFrame.RegisterCallback<ClickEvent>(e => ItemClick(e, item.Key, desc));
  111. itemListView.Add(itemFrame);
  112. }
  113. }
  114. // 点击商品后跳出确认窗口
  115. void ItemClick(ClickEvent clickEvent, string itemId, string description=null)
  116. {
  117. selectedItemId = itemId;
  118. msgBody.text = description;
  119. msgRoot.visible = true;
  120. }
  121. void BackBtnClick()
  122. {
  123. HomeSoundEffectController.Instance.PlaySoundEffect(2);
  124. var uiPlaceholder = GameObject.Find("UI Placeholder");
  125. if (uiPlaceholder != null)
  126. {
  127. var warehouseUI = uiPlaceholder.transform.Find("Warehouse").gameObject;
  128. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  129. warehouseUI.SetActive(false);
  130. vamUI.SetActive(true);
  131. }
  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. HomeSoundEffectController.Instance.PlaySoundEffect(0);
  142. // 这里btn.name必须和json里面商品类别完全匹配
  143. //GetItemList(btn.name);
  144. InstallItems(btn.name);
  145. }
  146. // msg no button 点击
  147. void MsgNoClick()
  148. {
  149. msgRoot.visible = false;
  150. }
  151. // msg yes button 点击
  152. // todo 以后在这里添加调出确认页面,确认后将道具使用请求给服务器
  153. void MsgYesClick()
  154. {
  155. //Debug.Log("msg yes clicked");
  156. msgRoot.visible = false;
  157. //ItemUseController itemUseController = new ItemUseController();
  158. ItemUseController.ItemUsed(selectedItemId);
  159. HomeSoundEffectController.Instance.PlaySoundEffect(1);
  160. // 关闭菜单
  161. var uiPlaceholder = GameObject.Find("UI Placeholder");
  162. if (uiPlaceholder != null)
  163. {
  164. var warehouseUI = uiPlaceholder.transform.Find("Warehouse").gameObject;
  165. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  166. warehouseUI.SetActive(false);
  167. vamUI.SetActive(true);
  168. }
  169. }
  170. }
  171. public class ItemInWarehouse
  172. {
  173. public string id;
  174. public string description;
  175. public string picture;
  176. public string qty;
  177. public ItemInWarehouse(string id, string desc, string qty, string picture)
  178. {
  179. this.id = id;
  180. this.description = desc;
  181. this.qty = qty;
  182. this.picture = picture;
  183. }
  184. }