_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Scene_shop/shopPanel.cs

57 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class shopPanel: ui_huodong
{
public List<itemInfo> itemInfos = new List<itemInfo>();
[Header("商店的详细面板")]
public GameObject Panel;
public override void Start()
{
base.Start();
initShop();
}
void initShop()
{
int index = 0;
// 遍历 shopItemDic 中的每个商品,并更新 itemInfos 中的显示项
foreach (KeyValuePair<int, ProductPro> go in Scene_main_jiekou.instance.shopItemDic)
{
// 如果 index 超过 itemInfos 的大小,表示字典的元素比 itemInfos 更多
if (index >= itemInfos.Count)
{
Debug.LogWarning("shopItemDic contains more items than itemInfos. Some items will not be shown.");
break; // 如果字典中的元素多于 itemInfos 中的项,停止遍历
}
// 更新当前 itemInfo 显示项
itemInfos[index].gameObject.SetActive(true); // 显示当前项
itemInfos[index].initShow(go.Value.ProductId,go.Value.ProductPic, go.Value.ProductName,go.Value.Price.ToString());
itemInfos[index].GetComponent<Button>().onClick.AddListener(async()=> {
await Scene_main_jiekou.instance.ShopItemDetail(go.Value.ProductId);
GameObject.Instantiate(Panel, GameObject.Find("Canvas").transform).GetComponent<wokePanel>().initShow(go.Value.ProductId, go.Value.ProductPic, go.Value.ProductName, go.Value.ProductName, go.Value.Price.ToString(), Scene_main_jiekou.instance._getProductResponse.Data.ProductName+":测试文本");
});
index++;
}
// 隐藏剩余未更新的 itemInfos 项
while (index < itemInfos.Count)
{
itemInfos[index].gameObject.SetActive(false); // 隐藏多余项
index++;
}
}
}