using Assets.Framework.Tool; using Assets.MyGame.Scripts.Net; using Assets.MyGame.Scripts.Net.API.UserData_SubClass; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class UIHome_BY_BackPackPanel : SubUIBase { public List props; public Dictionary propsID_Dic = new Dictionary(); public Dictionary propsObjHashCode_Dic = new Dictionary(); public Transform pageParent; public BackPackPropItem prefabItem; public TextMeshProUGUI tMP_Gold; public TextMeshProUGUI tMP_Diamond; [Header("道具详情")] public TextMeshProUGUI tMP_Name; public TextMeshProUGUI tMP_Description; public Image img_Prop; public string propID; public PropEnum propType; public ScrollRect scrollRect; public Button btn_Use; // 刷新时保存的状态 private Vector2 savedScrollPos; private string savedPropID; public override void Awake() { base.Awake(); GameManager.Instance.OnUserDataChanged += OnUserDataChanged; } private void OnUserDataChanged() { // 刷新前保存状态 if (scrollRect != null) savedScrollPos = scrollRect.normalizedPosition; savedPropID = propID; RefreshUI(); RebuildPropsList(); // 刷新后恢复状态 if (scrollRect != null) scrollRect.normalizedPosition = savedScrollPos; } protected override void OnShow(object args = null) { base.OnShow(args); RefreshUI(); RebuildPropsList(); } private void RefreshUI() { if (GameManager.Instance?.user?.assets == null) return; tMP_Gold.text = GameManager.Instance.user.assets.gold.ToString(); tMP_Diamond.text = GameManager.Instance.user.assets.diamond.ToString(); } private void RebuildPropsList() { props = GameManager.Instance.user.props; // 构建字典,过滤数量 ≤0 的 propsID_Dic.Clear(); if (props != null) { foreach (BaseProductCount product in props) { if (product.count > 0) propsID_Dic[product.number] = product; } } // 清空旧列表 for (int j = pageParent.childCount - 1; j >= 0; j--) Destroy(pageParent.GetChild(j).gameObject); propsObjHashCode_Dic.Clear(); bool isfirst = true; foreach (BaseProductCount product in propsID_Dic.Values) { // 第一项或与保存的选中id匹配 → 设为选中详情 if (isfirst || product.number == savedPropID) { ShowPropDetail(product); isfirst = false; } GameObject obj = Instantiate(prefabItem.gameObject, pageParent); var tempItem = obj.GetComponent(); tempItem.Init(APIManager.baseURL + product.imageUrl, product.count, (PropEnum)product.type); propsObjHashCode_Dic[obj.GetHashCode()] = product; string capturedNum = product.number; obj.GetComponent().onValueChanged.AddListener((value) => { if (value) ShowPropDetail(product); }); } } private void ShowPropDetail(BaseProductCount product) { if (product == null) return; tMP_Name.text = product.name; tMP_Description.text = product.desc; propID = product.number; propType = (PropEnum)product.type; WebTool.DownImage(APIManager.baseURL + product.imageUrl, (texture) => { if (texture != null && img_Prop != null) img_Prop.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f); }); btn_Use.onClick.RemoveAllListeners(); btn_Use.onClick.AddListener(() => { switch (propType) { case PropEnum.none: break; case PropEnum.炮塔碎片: break; case PropEnum.翅膀碎片: break; case PropEnum.战神碎片: break; case PropEnum.刮刮卡: uIManager.ShowAsync(UIPanel.GuaGuaKa); break; case PropEnum.改名卡: break; case PropEnum.大喇叭: break; case PropEnum.技能: break; case PropEnum.召唤卡: break; default: break; } }); } }