123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using Newtonsoft.Json;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEditor.UIElements;
- using UnityEngine;
- using UnityEngine.UIElements;
- // 这个controller 是用于控制 Shopping UI菜单的
- public class ShoppingController : MonoBehaviour
- {
- // Start is called before the first frame update
- // 主页面元素
- private Button foodButton, toyButton, otherButton, backButton;
- private Label coinQty;
- private List<ItemInShop> shoppingItems = 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);
- var coinArea = root.Q<VisualElement>("coinArea");
- coinQty = coinArea.Q<Label>("coinQty");
- 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)
- {
- // 按照类别获取shopping items
- shoppingItems.Clear();
- string itemRawData = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "shoppingUI", "item", type});
- Dictionary<string, object> itemDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemRawData);
- foreach (string _item in itemDict.Keys)
- {
- var itemDetail =JsonConvert.DeserializeObject<Dictionary<string, object>>(itemDict[_item].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();
- ItemInShop SearchedItem = new(_item, description, price, picture);
- shoppingItems.Add(SearchedItem);
- }
- }
- // 将items装载到菜单中,切换商品分类之后,需要重新读取和加载
- void InstallItems(string type)
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- itemListView.Clear();
- VisualTreeAsset itemResource = Resources.Load<VisualTreeAsset>("Shopping/Item");
- List<ItemInShop> items = new();
- foreach(var item in shoppingItems)
- {
- var itemFrame = new VisualElement();
- itemFrame = itemResource.CloneTree();
- var description = itemFrame.Q<Label>("description");
- description.text = item.description;
- var price = itemFrame.Q<Label>("price");
- price.text = item.price;
- 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()
- {
- // 游戏金币数量读取
- coinQty.text = UserProperty.coin.ToString();
- }
-
- 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 ItemInShop
- {
- public string id;
- public string description;
- public string picture;
- public string price;
- public ItemInShop(string id, string desc, string price, string picture) {
- this.id = id;
- this.description = desc;
- this.price = price;
- this.picture = picture;
- }
- }
|