Files
5.24BuYu/Assets/MyGame/aa/UI/Home/Sub/UIHome_BY_BackPackPanel.cs
T

183 lines
5.9 KiB
C#
Raw Normal View History

2026-06-26 17:26:10 +08:00
using Assets.Framework.Tool;
2026-04-13 18:30:55 +08:00
using Assets.MyGame.Scripts.Net;
2026-03-27 15:18:35 +08:00
using Assets.MyGame.Scripts.Net.API.UserData_SubClass;
using System.Collections;
2025-12-13 18:18:28 +08:00
using System.Collections.Generic;
2026-03-27 15:18:35 +08:00
using TMPro;
2025-12-13 18:18:28 +08:00
using UnityEngine;
2026-03-27 15:18:35 +08:00
using UnityEngine.UI;
2025-12-13 18:18:28 +08:00
2025-12-26 11:15:56 +08:00
public class UIHome_BY_BackPackPanel : SubUIBase
2025-12-13 18:18:28 +08:00
{
2026-03-27 15:18:35 +08:00
public List<BaseProductCount> props;
2026-06-26 17:26:10 +08:00
public Dictionary<string, BaseProductCount> propsID_Dic = new Dictionary<string, BaseProductCount>();
public Dictionary<int, BaseProductCount> propsObjHashCode_Dic = new Dictionary<int, BaseProductCount>();
public Transform pageParent;
2026-06-25 01:48:07 +08:00
public BackPackPropItem prefabItem;
2026-03-27 15:18:35 +08:00
2026-06-25 16:18:07 +08:00
public TextMeshProUGUI tMP_Gold;
public TextMeshProUGUI tMP_Diamond;
2026-06-25 01:48:07 +08:00
[Header("道具详情")]
2026-03-27 15:18:35 +08:00
public TextMeshProUGUI tMP_Name;
public TextMeshProUGUI tMP_Description;
2026-06-25 01:48:07 +08:00
public Image img_Prop;
2026-06-26 17:26:10 +08:00
public string propID;
public PropEnum propType;
2026-03-27 15:18:35 +08:00
public ScrollRect scrollRect;
2026-06-25 01:48:07 +08:00
public Button btn_Use;
2026-06-26 17:26:10 +08:00
2026-07-07 02:22:31 +08:00
[Header("改名")]//使用改名卡后,弹出输入框,输入新名字,点击确定按钮,发送请求修改名字
public GameObject changNamePanel;
public TMP_InputField tMPText_NewName;
public Button btn_ChangeName;
2026-06-26 17:26:10 +08:00
// 刷新时保存的状态
2026-07-06 03:04:08 +08:00
//private Vector2 savedScrollPos;
2026-06-26 17:26:10 +08:00
private string savedPropID;
2026-03-27 15:18:35 +08:00
public override void Awake()
2025-12-13 18:18:28 +08:00
{
2026-03-27 15:18:35 +08:00
base.Awake();
2026-06-26 17:26:10 +08:00
GameManager.Instance.OnUserDataChanged += OnUserDataChanged;
2026-07-07 02:22:31 +08:00
// 改名确认按钮
if (btn_ChangeName != null)
btn_ChangeName.onClick.AddListener(OnClickConfirmChangeName);
// 改名面板默认关闭
if (changNamePanel != null)
changNamePanel.SetActive(false);
}
private void OnClickConfirmChangeName()
{
string newName = tMPText_NewName?.text?.Trim();
if (string.IsNullOrEmpty(newName))
{
UIManager.Instance.ShowBoardError("请输入新名字");
return;
}
APIRequestList.UpdateUserName(newName, (result) =>
{
UIManager.Instance.ShowBoardError("改名成功");
GameManager.Instance.user.BaseInfo.userName = newName;
tMPText_NewName.text = "";
changNamePanel.SetActive(false);
});
2025-12-13 18:18:28 +08:00
}
2026-06-26 17:26:10 +08:00
private void OnUserDataChanged()
{
// 刷新前保存状态
2026-07-06 03:04:08 +08:00
//if (scrollRect != null) savedScrollPos = scrollRect.normalizedPosition;
2026-06-26 17:26:10 +08:00
savedPropID = propID;
RefreshUI();
RebuildPropsList();
// 刷新后恢复状态
2026-07-06 03:04:08 +08:00
//if (scrollRect != null) scrollRect.normalizedPosition = savedScrollPos;
2026-06-26 17:26:10 +08:00
}
2026-03-27 15:18:35 +08:00
protected override void OnShow(object args = null)
2025-12-13 18:18:28 +08:00
{
2026-03-27 15:18:35 +08:00
base.OnShow(args);
2026-06-26 17:26:10 +08:00
RefreshUI();
RebuildPropsList();
}
2026-04-17 18:30:51 +08:00
2026-06-26 17:26:10 +08:00
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;
2026-03-27 15:18:35 +08:00
}
}
2026-06-25 01:48:07 +08:00
2026-06-26 17:26:10 +08:00
// 清空旧列表
for (int j = pageParent.childCount - 1; j >= 0; j--)
Destroy(pageParent.GetChild(j).gameObject);
propsObjHashCode_Dic.Clear();
bool isfirst = true;
2026-03-27 15:18:35 +08:00
foreach (BaseProductCount product in propsID_Dic.Values)
{
2026-06-26 17:26:10 +08:00
// 第一项或与保存的选中id匹配 → 设为选中详情
if (isfirst || product.number == savedPropID)
2026-06-25 01:48:07 +08:00
{
2026-06-26 17:26:10 +08:00
ShowPropDetail(product);
2026-06-25 01:48:07 +08:00
isfirst = false;
}
2026-06-26 17:26:10 +08:00
2026-06-25 01:48:07 +08:00
GameObject obj = Instantiate(prefabItem.gameObject, pageParent);
var tempItem = obj.GetComponent<BackPackPropItem>();
tempItem.Init(APIManager.baseURL + product.imageUrl, product.count, (PropEnum)product.type);
2026-06-26 17:26:10 +08:00
propsObjHashCode_Dic[obj.GetHashCode()] = product;
string capturedNum = product.number;
2026-03-27 15:18:35 +08:00
obj.GetComponent<Toggle>().onValueChanged.AddListener((value) =>
{
2026-06-26 17:26:10 +08:00
if (value) ShowPropDetail(product);
2026-03-27 15:18:35 +08:00
});
}
2026-06-27 21:12:04 +08:00
// 默认选中第一项(isOn 触发 onValueChanged 自动显示详情)
if (pageParent.childCount > 0)
pageParent.GetChild(0).GetComponent<Toggle>().isOn = true;
2026-06-26 17:26:10 +08:00
}
2026-03-27 15:18:35 +08:00
2026-06-26 17:26:10 +08:00
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;
2026-07-07 02:22:31 +08:00
case PropEnum.改名卡:
if (changNamePanel != null)
changNamePanel.SetActive(true);
break;
2026-06-26 17:26:10 +08:00
case PropEnum.大喇叭: break;
case PropEnum.技能: break;
case PropEnum.召唤卡: break;
default: break;
}
});
2025-12-13 18:18:28 +08:00
}
}