WarehouseController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. if (item.Value > 999)
  113. {
  114. price.text = "999+";
  115. }
  116. else
  117. {
  118. price.text = item.Value.ToString();
  119. }
  120. // 读取图片
  121. string picturePath = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", type, item.Key, "picture" });
  122. var texture = Resources.Load<Texture2D>(picturePath);
  123. var picture = itemFrame.Q<VisualElement>("picture");
  124. picture.style.backgroundImage = new StyleBackground(texture);
  125. itemFrame.RegisterCallback<ClickEvent>(e => ItemClick(e, item.Key, desc));
  126. itemListView.Add(itemFrame);
  127. }
  128. }
  129. }
  130. // 点击商品后跳出确认窗口
  131. void ItemClick(ClickEvent clickEvent, string itemId, string description = null)
  132. {
  133. selectedItemId = itemId;
  134. string targetType = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "item", currentTab, itemId, "target" });
  135. targetType = currentTab + "_" + targetType;
  136. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "warehouseUI", "message", targetType, EnviromentSetting.languageCode });
  137. if (msg.Contains("<<dog_name>>"))
  138. {
  139. msg = msg.Replace("<<dog_name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  140. }
  141. if (msg.Contains("<<item_name>>"))
  142. {
  143. msg = msg.Replace("<<item_name>>", description);
  144. }
  145. msgBody.text = msg;
  146. msgRoot.visible = true;
  147. }
  148. void BackBtnClick()
  149. {
  150. HomeSoundEffectController.Instance.PlaySoundEffect(2);
  151. var uiPlaceholder = GameObject.Find("UI Placeholder");
  152. if (uiPlaceholder != null)
  153. {
  154. var warehouseUI = uiPlaceholder.transform.Find("Warehouse").gameObject;
  155. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  156. warehouseUI.SetActive(false);
  157. vamUI.SetActive(true);
  158. }
  159. }
  160. void TabSwitch(Button btn)
  161. {
  162. itemListView.style.backgroundColor = btn.resolvedStyle.backgroundColor;
  163. foodButton.transform.scale = new Vector3(1f, 1f, 1);
  164. toyButton.transform.scale = new Vector3(1f, 1f, 1);
  165. otherButton.transform.scale = new Vector3(1f, 1f, 1);
  166. btn.transform.scale = new Vector3(1.2f, 1.2f, 1);
  167. itemListView.Clear();
  168. HomeSoundEffectController.Instance.PlaySoundEffect(0);
  169. // 这里btn.name必须和json里面商品类别完全匹配
  170. //GetItemList(btn.name);
  171. InstallItems(btn.name);
  172. }
  173. // msg no button 点击
  174. void MsgNoClick()
  175. {
  176. msgRoot.visible = false;
  177. }
  178. void MsgYesClick()
  179. {
  180. //Debug.Log("msg yes clicked");
  181. msgRoot.visible = false;
  182. //ItemUseController itemUseController = new ItemUseController();
  183. ItemUseController.ItemUsed(selectedItemId);
  184. HomeSoundEffectController.Instance.PlaySoundEffect(1);
  185. // 关闭菜单
  186. var uiPlaceholder = GameObject.Find("UI Placeholder");
  187. if (uiPlaceholder != null)
  188. {
  189. var warehouseUI = uiPlaceholder.transform.Find("Warehouse").gameObject;
  190. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  191. warehouseUI.SetActive(false);
  192. vamUI.SetActive(true);
  193. }
  194. }
  195. }
  196. public class ItemInWarehouse
  197. {
  198. public string id;
  199. public string description;
  200. public string picture;
  201. public string qty;
  202. public ItemInWarehouse(string id, string desc, string qty, string picture)
  203. {
  204. this.id = id;
  205. this.description = desc;
  206. this.qty = qty;
  207. this.picture = picture;
  208. }
  209. }