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

248 lines
8.3 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-07-09 14:29:17 +08:00
[Header("大喇叭")]//使用大喇叭后,弹出输入框,输入弹幕,点击确定按钮,发送请求
public GameObject DaLaBaPanel;
public TMP_InputField tMPText_DaLaBaLabel;
public Button btn_FaSong;
2026-07-25 01:11:49 +08:00
[Header("使用提示(带数量)")]
public Slider slider_UseNum;
public TextMeshProUGUI tMPText_ShiyongLabel;//确认使用道具 propname*<color=yellow>数量</color>
public Button btn_Add;
public Button btn_Subtract;
public Button btn_QueRen;
2026-07-09 14:29:17 +08:00
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);
2026-07-09 14:29:17 +08:00
// 大喇叭
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);
});
2026-07-07 02:22:31 +08:00
}
private void OnClickConfirmChangeName()
{
string newName = tMPText_NewName?.text?.Trim();
if (string.IsNullOrEmpty(newName))
{
UIManager.Instance.ShowBoardError("请输入新名字");
return;
}
2026-07-10 00:46:23 +08:00
UIManager.Instance.ShowTiShi($"确定要将名字改为\"{newName}\"吗?", () =>
2026-07-07 02:22:31 +08:00
{
2026-07-10 00:46:23 +08:00
APIRequestList.UpdateUserName(newName, (result) =>
{
UIManager.Instance.ShowBoardError("改名成功");
tMPText_NewName.text = "";
changNamePanel.SetActive(false);
2026-07-23 01:51:07 +08:00
// 拉取服务器最新用户数据,同步本地并触发全局 UI 更新
APIRequestList.GetUser((user) =>
{
if (user != null)
GameManager.Instance.UpdataUserData(user);
});
2026-07-10 00:46:23 +08:00
});
2026-07-07 02:22:31 +08:00
});
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>();
2026-07-09 19:35:25 +08:00
tempItem.Init(new BackPackPropItemData
{
url = APIManager.baseURL + product.imageUrl,
count = product.count,
propEnum = (PropEnum)product.type
});
2026-06-25 01:48:07 +08:00
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-07-09 14:29:17 +08:00
case PropEnum.大喇叭:
if (DaLaBaPanel != null)
DaLaBaPanel.SetActive(true);
break;
2026-06-26 17:26:10 +08:00
case PropEnum.技能: break;
case PropEnum.召唤卡: break;
2026-07-16 20:16:05 +08:00
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;
2026-06-26 17:26:10 +08:00
default: break;
}
});
2025-12-13 18:18:28 +08:00
}
}