WarehouseController.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 TextField qtyField;
  20. private VisualElement msgRoot, msgField;
  21. // 选中的产品
  22. private string selectedItemId;
  23. private string currentTab;
  24. void OnEnable()
  25. {
  26. // 基层元素获取
  27. var root = GetComponent<UIDocument>().rootVisualElement;
  28. var mainManu = root.Q<VisualElement>("mainManu");
  29. itemListView = root.Q<VisualElement>("itemListView");
  30. foodButton = mainManu.Q<Button>("food");
  31. foodButton.clicked += () => TabSwitch(foodButton);
  32. toyButton = mainManu.Q<Button>("toy");
  33. toyButton.clicked += () => TabSwitch(toyButton);
  34. otherButton = mainManu.Q<Button>("other");
  35. otherButton.clicked += () => TabSwitch(otherButton);
  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. yesButton.clicked += MsgYesClick;
  45. noButton = msgField.Q<Button>("msgNo");
  46. noButton.clicked += MsgNoClick;
  47. qtyField = msgField.Q<TextField>("qtyField");
  48. //初始化设定
  49. LanguageSetting();
  50. foodButton.transform.scale = new Vector3(1.2f, 1.2f);
  51. InstallItems("food");
  52. qtyField.visible = false;
  53. }
  54. private void OnDisable()
  55. {
  56. warehouseItems.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. // 将items装载到菜单中,切换商品分类之后,需要重新读取和加载
  80. void InstallItems(string type)
  81. {
  82. currentTab = type;
  83. var root = GetComponent<UIDocument>().rootVisualElement;
  84. itemListView.Clear();
  85. VisualTreeAsset itemResource = Resources.Load<VisualTreeAsset>("Shopping/Item");
  86. Dictionary<string, int> items = new();
  87. if (type == "food")
  88. {
  89. items = UserProperty.food;
  90. }
  91. else if (type == "toy")
  92. {
  93. items = UserProperty.toy;
  94. }
  95. else if (type == "other")
  96. {
  97. items = UserProperty.other;
  98. }
  99. foreach (var item in items)
  100. {
  101. if (item.Value > 0)
  102. {
  103. var itemFrame = new VisualElement();
  104. itemFrame = itemResource.CloneTree();
  105. // 从language.json读取item的文字描述
  106. string desc = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", type, item.Key, "description", EnviromentSetting.languageCode });
  107. var description = itemFrame.Q<Label>("description");
  108. description.text = desc;
  109. // 这里复用 shopping UI 里面的 item.uxml
  110. // UI 里面命名为price,实际是qty
  111. var price = itemFrame.Q<Label>("price");
  112. price.text = item.Value.ToString();
  113. // 读取图片
  114. string picturePath = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", type, item.Key, "picture" });
  115. var texture = Resources.Load<Texture2D>(picturePath);
  116. var picture = itemFrame.Q<VisualElement>("picture");
  117. picture.style.backgroundImage = new StyleBackground(texture);
  118. itemFrame.RegisterCallback<ClickEvent>(e => ItemClick(e, item.Key, desc));
  119. itemListView.Add(itemFrame);
  120. }
  121. }
  122. }
  123. // 点击商品后跳出确认窗口
  124. void ItemClick(ClickEvent clickEvent, string itemId, string description = null)
  125. {
  126. selectedItemId = itemId;
  127. string targetType = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", currentTab, itemId, "target" });
  128. targetType = currentTab + "_" + targetType;
  129. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "warehouseUI", "message", targetType, EnviromentSetting.languageCode });
  130. if (msg.Contains("<<dog_name>>"))
  131. {
  132. msg = msg.Replace("<<dog_name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  133. }
  134. if (msg.Contains("<<item_name>>"))
  135. {
  136. msg = msg.Replace("<<item_name>>", description);
  137. }
  138. msgBody.text = msg;
  139. msgRoot.visible = true;
  140. }
  141. void BackBtnClick()
  142. {
  143. HomeSoundEffectController.Instance.PlaySoundEffect(2);
  144. var uiPlaceholder = GameObject.Find("UI Placeholder");
  145. if (uiPlaceholder != null)
  146. {
  147. var warehouseUI = uiPlaceholder.transform.Find("Warehouse").gameObject;
  148. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  149. warehouseUI.SetActive(false);
  150. vamUI.SetActive(true);
  151. }
  152. }
  153. void TabSwitch(Button btn)
  154. {
  155. itemListView.style.backgroundColor = btn.resolvedStyle.backgroundColor;
  156. foodButton.transform.scale = new Vector3(1f, 1f, 1);
  157. toyButton.transform.scale = new Vector3(1f, 1f, 1);
  158. otherButton.transform.scale = new Vector3(1f, 1f, 1);
  159. btn.transform.scale = new Vector3(1.2f, 1.2f, 1);
  160. itemListView.Clear();
  161. HomeSoundEffectController.Instance.PlaySoundEffect(0);
  162. // 这里btn.name必须和json里面商品类别完全匹配
  163. //GetItemList(btn.name);
  164. InstallItems(btn.name);
  165. }
  166. // msg no button 点击
  167. void MsgNoClick()
  168. {
  169. msgRoot.visible = false;
  170. }
  171. void MsgYesClick()
  172. {
  173. //Debug.Log("msg yes clicked");
  174. msgRoot.visible = false;
  175. //ItemUseController itemUseController = new ItemUseController();
  176. ItemUseController.ItemUsed(selectedItemId);
  177. HomeSoundEffectController.Instance.PlaySoundEffect(1);
  178. // 关闭菜单
  179. var uiPlaceholder = GameObject.Find("UI Placeholder");
  180. if (uiPlaceholder != null)
  181. {
  182. var warehouseUI = uiPlaceholder.transform.Find("Warehouse").gameObject;
  183. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  184. warehouseUI.SetActive(false);
  185. vamUI.SetActive(true);
  186. }
  187. }
  188. }
  189. public class ItemInWarehouse
  190. {
  191. public string id;
  192. public string description;
  193. public string picture;
  194. public string qty;
  195. public ItemInWarehouse(string id, string desc, string qty, string picture)
  196. {
  197. this.id = id;
  198. this.description = desc;
  199. this.qty = qty;
  200. this.picture = picture;
  201. }
  202. }