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; [Header("改名")]//使用改名卡后,弹出输入框,输入新名字,点击确定按钮,发送请求修改名字 public GameObject changNamePanel; public TMP_InputField tMPText_NewName; public Button btn_ChangeName; [Header("大喇叭")]//使用大喇叭后,弹出输入框,输入弹幕,点击确定按钮,发送请求 public GameObject DaLaBaPanel; public TMP_InputField tMPText_DaLaBaLabel; public Button btn_FaSong; [Header("使用提示(带数量)")] public Slider slider_UseNum; public TextMeshProUGUI tMPText_ShiyongLabel;//确认使用道具 propname*数量 public Button btn_Add; public Button btn_Subtract; public Button btn_QueRen; // 刷新时保存的状态 //private Vector2 savedScrollPos; private string savedPropID; public override void Awake() { base.Awake(); GameManager.Instance.OnUserDataChanged += OnUserDataChanged; // 改名确认按钮 if (btn_ChangeName != null) btn_ChangeName.onClick.AddListener(OnClickConfirmChangeName); // 改名面板默认关闭 if (changNamePanel != null) changNamePanel.SetActive(false); // 大喇叭 if (DaLaBaPanel != null) DaLaBaPanel.SetActive(false); if (btn_FaSong != null) btn_FaSong.onClick.AddListener(OnClickFaSong); } private void OnClickFaSong() { string msg = "广播:"+ tMPText_DaLaBaLabel?.text?.Trim(); if (string.IsNullOrEmpty(msg)) { UIManager.Instance.ShowBoardError("请输入广播内容"); return; } APIRequestList.SendBroadcast(msg, (result) => { //UIManager.Instance.ShowBoardGongGao($"广播: {msg}"); tMPText_DaLaBaLabel.text = ""; DaLaBaPanel.SetActive(false); }); } private void OnClickConfirmChangeName() { string newName = tMPText_NewName?.text?.Trim(); if (string.IsNullOrEmpty(newName)) { UIManager.Instance.ShowBoardError("请输入新名字"); return; } UIManager.Instance.ShowTiShi($"确定要将名字改为\"{newName}\"吗?", () => { APIRequestList.UpdateUserName(newName, (result) => { UIManager.Instance.ShowBoardError("改名成功"); tMPText_NewName.text = ""; changNamePanel.SetActive(false); // 拉取服务器最新用户数据,同步本地并触发全局 UI 更新 APIRequestList.GetUser((user) => { if (user != null) GameManager.Instance.UpdataUserData(user); }); }); }); } 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(new BackPackPropItemData { url = APIManager.baseURL + product.imageUrl, count = product.count, propEnum = (PropEnum)product.type }); propsObjHashCode_Dic[obj.GetHashCode()] = product; string capturedNum = product.number; obj.GetComponent().onValueChanged.AddListener((value) => { if (value) ShowPropDetail(product); }); } // 默认选中第一项(isOn 触发 onValueChanged 自动显示详情) if (pageParent.childCount > 0) pageParent.GetChild(0).GetComponent().isOn = true; } 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.改名卡: if (changNamePanel != null) changNamePanel.SetActive(true); break; case PropEnum.大喇叭: if (DaLaBaPanel != null) DaLaBaPanel.SetActive(true); break; case PropEnum.技能: break; case PropEnum.召唤卡: break; case PropEnum.VIP经验: UIManager.Instance.ShowTiShi($"确定要使用VIP经验包吗?", () => { APIRequestList.UseVipExp((result) => { UIManager.Instance.ShowBoardError("使用成功"); // 刷新用户数据 APIRequestList.GetUser(GameManager.Instance.user.id, (user) => { GameManager.Instance.UpdataUserData(user); }); }); }); break; default: break; } }); } }