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 string propName; // 当前选中道具名称(数量选择面板用) 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 GameObject useObj; public Slider slider_UseNum; public TextMeshProUGUI tMPText_ShiyongLabel;//确认使用道具 propname*数量 public Button btn_Add; public Button btn_Subtract; public Button btn_QueRen; /// 当前选中道具的拥有数量(用于数量选择面板上限) private int _curPropOwnedCount; /// 当前选中数量选择面板对应道具的回调(确认使用时调用) private System.Action _curUseCountAction; // 刷新时保存的状态 //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); // 数量选择面板(默认隐藏) if (useObj != null) useObj.SetActive(false); if (slider_UseNum != null) slider_UseNum.onValueChanged.AddListener(v => UpdateUseNumLabel()); if (btn_Add != null) btn_Add.onClick.AddListener(() => { if (slider_UseNum != null) slider_UseNum.value = Mathf.Min((int)slider_UseNum.value + 1, (int)slider_UseNum.maxValue); }); if (btn_Subtract != null) btn_Subtract.onClick.AddListener(() => { if (slider_UseNum != null) slider_UseNum.value = Mathf.Max((int)slider_UseNum.value - 1, (int)slider_UseNum.minValue); }); if (btn_QueRen != null) btn_QueRen.onClick.AddListener(OnClickConfirmUseNum); } /// 刷新数量选择标签 private void UpdateUseNumLabel() { if (tMPText_ShiyongLabel == null) return; int n = slider_UseNum != null ? Mathf.RoundToInt(slider_UseNum.value) : 1; tMPText_ShiyongLabel.text = $"确认使用道具 {propName} * {n}"; } /// 确认使用按钮:执行当前数量回调 private void OnClickConfirmUseNum() { int n = slider_UseNum != null ? Mathf.RoundToInt(slider_UseNum.value) : 1; if (n <= 0) { UIManager.Instance.ShowBoardError("数量必须大于0"); return; } var cb = _curUseCountAction; HideUseNumPanel(); cb?.Invoke(n); } /// 显示数量选择面板 /// 当前拥有数量(滑块上限) /// 确认使用回调(参数=使用数量) private void ShowUseNumPanel(int ownedCount, System.Action onConfirm) { _curPropOwnedCount = ownedCount; _curUseCountAction = onConfirm; if (useObj != null) useObj.SetActive(true); if (slider_UseNum != null) { slider_UseNum.minValue = 1; slider_UseNum.maxValue = Mathf.Max(1, ownedCount); slider_UseNum.value = 1; slider_UseNum.wholeNumbers = true; } UpdateUseNumLabel(); } /// 隐藏数量选择面板 private void HideUseNumPanel() { if (useObj != null) useObj.SetActive(false); _curUseCountAction = null; } 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(); Toggle firstToggle = null; Toggle savedToggle = null; foreach (BaseProductCount product in propsID_Dic.Values) { 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 }); var toggle = obj.GetComponent(); propsObjHashCode_Dic[obj.GetHashCode()] = product; if (firstToggle == null) firstToggle = toggle; if (!string.IsNullOrEmpty(savedPropID) && product.number == savedPropID) savedToggle = toggle; obj.GetComponent().onValueChanged.AddListener((value) => { if (value) ShowPropDetail(product); }); } // 优先选中刷新前的道具;若已用完则选中第一个 var target = savedToggle ?? firstToggle; if (target != null) target.isOn = true; } private void ShowPropDetail(BaseProductCount product) { if (product == null) return; tMP_Name.text = product.name; tMP_Description.text = product.desc; propID = product.number; propName = product.name; 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经验: // 数量选择面板:滑块上限=当前拥有数量 ShowUseNumPanel(product.count, n => { APIRequestList.UseVipExp(n, (result) => { UIManager.Instance.ShowBoardError("使用成功"); APIRequestList.GetUser(GameManager.Instance.user.id, (user) => { GameManager.Instance.UpdataUserData(user); }); }); }); break; default: break; } }); } }