123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using Newtonsoft.Json;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEditor.UIElements;
- using UnityEngine;
- using UnityEngine.UIElements;
- // 这个controller 是用于控制 Warehouse UI菜单的
- public class WarehouseController : MonoBehaviour
- {
- // Start is called before the first frame update
- // 主页面元素
- private Button foodButton, toyButton, otherButton, backButton;
- private List<ItemInWarehouse> warehouseItems = new();
- private VisualElement itemListView;
- //弹窗元素
- private Button yesButton, noButton;
- private Label msgBody;
- private VisualElement msgRoot, msgField;
- // 选中的产品
- private string selectedItemId;
- void Start()
- {
- // 基层元素获取
- var root = GetComponent<UIDocument>().rootVisualElement;
- var mainManu = root.Q<VisualElement>("mainManu");
- itemListView = root.Q<VisualElement>("itemListView");
- foodButton = mainManu.Q<Button>("food");
- foodButton.clicked += () => TabSwitch(foodButton);
- toyButton = mainManu.Q<Button>("toy");
- toyButton.clicked += () => TabSwitch(toyButton);
- otherButton = mainManu.Q<Button>("other");
- otherButton.clicked += () => TabSwitch(otherButton);
- backButton = root.Q<Button>("back");
- // 弹窗元素获取
- msgRoot = root.Q<VisualElement>("msgRoot");
- msgField = msgRoot.Q<VisualElement>("msgField");
- msgBody = msgField.Q<Label>("msgBody");
- yesButton = msgField.Q<Button>("msgYes");
- noButton = msgField.Q<Button>("msgNo");
- noButton.clicked += MsgNoClick;
- //初始化设定
- LanguageSetting();
- //DataLoading();
- GetItemList("food");
- foodButton.transform.scale = new Vector3(1.2f, 1.2f);
- InstallItems("food");
- }
- // Update is called once per frame
- //void Update()
- //{
-
- //}
- //菜单语言设定
- void LanguageSetting()
- {
- // 设置 shopping UI界面里面label和按键的语言显示
- string textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "food", EnviromentSetting.languageCode });
- foodButton.text = textValue;
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "toy", EnviromentSetting.languageCode });
- toyButton.text = textValue;
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "other", EnviromentSetting.languageCode });
- otherButton.text = textValue;
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "button", "back", EnviromentSetting.languageCode });
- backButton.text = textValue;
- }
- //获取所有产品列表,切换商品分类之后,需要重新读取和加载
- void GetItemList(string type)
- {
- // 按照类别获取warehouse
- warehouseItems.Clear();
- // todo 下面这一端要重写。Warehouse获取清单方式和Shopping UI不同
- if(UserProperty.itemStocks.TryGetValue(type, out List < ItemStock > items)){
- //List<ItemStock> items = UserProperty.itemStocks[type];
- // itemDict获取当前分类下所有item
- string itemRawData = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "item", type });
- Dictionary<string, object> itemDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemRawData);
- foreach (ItemStock item in items)
- {
- var itemDetail = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDict[item.id].ToString());
- string price = itemDetail["price"].ToString();
- string picture = itemDetail["picture"].ToString();
- var desc = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDetail["description"].ToString());
- string description = desc[EnviromentSetting.languageCode].ToString();
- ItemInWarehouse searchedItem = new(item.id, description, price, picture);
- warehouseItems.Add(searchedItem);
- }
- }
- }
- // 将items装载到菜单中,切换商品分类之后,需要重新读取和加载
- void InstallItems(string type)
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- itemListView.Clear();
- VisualTreeAsset itemResource = Resources.Load<VisualTreeAsset>("Shopping/Item");
- List<ItemInWarehouse> items = new();
- foreach (var item in warehouseItems)
- {
- var itemFrame = new VisualElement();
- itemFrame = itemResource.CloneTree();
- var description = itemFrame.Q<Label>("description");
- description.text = item.description;
- // 这里复用 shopping UI 里面的 item.uxml
- // UI 里面命名为price,实际是qty
- var price = itemFrame.Q<Label>("price");
- price.text = item.qty;
- var texture = Resources.Load<Texture2D>(item.picture);
- var picture = itemFrame.Q<VisualElement>("picture");
- picture.style.backgroundImage = new StyleBackground(texture);
- itemFrame.RegisterCallback<ClickEvent>(e => ItemClick(e, item.id, item.description));
- itemListView.Add(itemFrame);
- }
- }
- // 点击商品后跳出确认窗口
- void ItemClick(ClickEvent clickEvent, string itemId, string description=null)
- {
- selectedItemId = itemId;
- msgBody.text = description;
- msgRoot.visible = true;
- }
- // 数据读取
- void DataLoading()
- {
-
- }
- void TabSwitch(Button btn)
- {
- itemListView.style.backgroundColor = btn.resolvedStyle.backgroundColor;
- foodButton.transform.scale = new Vector3(1f, 1f, 1);
- toyButton.transform.scale = new Vector3(1f, 1f, 1);
- otherButton.transform.scale = new Vector3(1f, 1f, 1);
- btn.transform.scale = new Vector3(1.2f, 1.2f, 1);
- itemListView.Clear();
- // 这里btn.name必须和json里面商品类别完全匹配
- GetItemList(btn.name);
- InstallItems(btn.name);
- }
- // msg no button 点击
- void MsgNoClick()
- {
- msgRoot.visible = false;
- }
- // msg yes button 点击
- // todo 以后在这里添加调出确认页面,确认后将道具使用请求给服务器
- void MsgYesClick()
- {
- Debug.Log("msg yes clicked");
- msgRoot.visible = false;
- }
- }
- public class ItemInWarehouse
- {
- public string id;
- public string description;
- public string picture;
- public string qty;
- public ItemInWarehouse(string id, string desc, string qty, string picture)
- {
- this.id = id;
- this.description = desc;
- this.qty = qty;
- this.picture = picture;
- }
- }
|