Files
5.24BuYu/Assets/MyGame/aa/UI/Home/Sub/UIHome_BY_VIPPanel.cs
T
2026-06-27 22:35:16 +08:00

208 lines
6.9 KiB
C#

using Assets.Framework.Tool;
using Assets.MyGame.Scripts.Net;
using Assets.MyGame.Scripts.Net.API.GameConfig_SubClass;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UIHome_BY_VIPPanel : SubUIBase
{
[Header("VIP等级-名字-介绍")]
public TextMeshProUGUI txt_VIPLevel;
public TextMeshProUGUI txt_VIPUpLeaveLabel;
public TextMeshProUGUI txt_VIPContent;
public TextMeshProUGUI txt_VIPEXP;
public Slider EXP;
[Header("Vip等级选项")]
public Toggle btn_LeaveItemPrefab;
public Transform btn_LeaveItemParent;
[Header("Vip领取奖励选项")]
public BackPackPropItem btn_AwardItemPrefab;
public Transform btn_AwardItemParent_Top;
public Transform btn_AwardItemParent_Bottom;
public TextMeshProUGUI txt_VIPNUnlock;
public TextMeshProUGUI txt_VIPNFreeGift;
public Button btn_Award_Get;
public GameObject btn_Award_GetEnd;
public GameObject btn_Award_NotUnlock;
public List<GameObject> img_VIPNumBG;
private int vipLevel = 0;
private List<VipLevelConfig> vipConfigs;
public override void Awake()
{
base.Awake();
vipConfigs = GameManager.Instance.gameConfig.vipLevelConfigs;
GameManager.Instance.OnUserDataChanged += OnUserDataChanged;
}
private void OnUserDataChanged()
{
vipLevel = GameManager.Instance.user.assets.vipLevel;
// 打开时 OnShow 会重建所有,这里不需要额外处理
}
protected override void OnShow(object args = null)
{
base.OnShow(args);
vipConfigs = GameManager.Instance.gameConfig.vipLevelConfigs;
if (vipConfigs == null || vipConfigs.Count == 0) return;
var assets = GameManager.Instance.user.assets;
vipLevel = assets.vipLevel;
// 先清除旧选项再重建
BuildLevelTabs();
// 通过设置 toggle.isOn 触发 onValueChanged → 自动调用 UpdateVIPPanel
for (int i = 0; i < btn_LeaveItemParent.childCount; i++)
{
if (vipConfigs[i].level == vipLevel)
{
btn_LeaveItemParent.GetChild(i).GetComponent<Toggle>().isOn = true;
break;
}
}
}
private void BuildLevelTabs()
{
// 清空旧选项
for (int i = btn_LeaveItemParent.childCount - 1; i >= 0; i--)
Destroy(btn_LeaveItemParent.GetChild(i).gameObject);
foreach (var item in vipConfigs)
{
var toggle = Instantiate(btn_LeaveItemPrefab, btn_LeaveItemParent);
int level = item.level;
// child(1) 显示 "贵族n"
toggle.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = $"贵族{level}";
toggle.onValueChanged.RemoveAllListeners();
toggle.onValueChanged.AddListener((isOn) =>
{
if (isOn) UpdateVIPPanel(level);
});
}
}
public void UpdateVIPPanel(int level)
{
var config = vipConfigs?.Find(v => v.level == level);
if (config == null) return;
txt_VIPLevel.text = $"{level}";
txt_VIPContent.text = config.remark;
// VIP等级背景切换
for (int i = 0; i < img_VIPNumBG.Count; i++)
{
if (img_VIPNumBG[i] != null)
img_VIPNumBG[i].SetActive(i == GetVIPBGIndex(level));
}
var assets = GameManager.Instance.user.assets;
var staticInfo = GameManager.Instance.user.@static;
// 经验/进度
double currentExp = staticInfo?.totalRecharge ?? 0;
EXP.maxValue = config.expMax;
EXP.minValue = config.expMin;
EXP.value = (float)currentExp;
txt_VIPEXP.text = $"{currentExp}/{config.expMax}";
// 再充金额
double needAmount = config.expMax > currentExp ? config.expMax - currentExp : 0;
txt_VIPUpLeaveLabel.text = $"再充值 <color=yellow>{needAmount}</color> 元即可升级至<color=yellow><size=36>贵族{level}</size></color>";
// 顶部一次性奖励
txt_VIPNUnlock.text = $"贵族{level}解锁";
ShowGiftItems(config.giftList, btn_AwardItemParent_Top);
// 底部每日免费礼包
txt_VIPNFreeGift.text = $"贵族{level}免费礼包";
ShowGiftItems(config.dailyGift, btn_AwardItemParent_Bottom);
// 领取按钮状态
if (level > vipLevel)
{
// 未解锁
btn_Award_Get.gameObject.SetActive(false);
btn_Award_GetEnd.SetActive(false);
btn_Award_NotUnlock.SetActive(true);
}
else
{
// 已解锁 → 显示领取按钮
btn_Award_Get.gameObject.SetActive(true);
btn_Award_GetEnd.SetActive(false);
btn_Award_NotUnlock.SetActive(false);
btn_Award_Get.onClick.RemoveAllListeners();
btn_Award_Get.onClick.AddListener(() =>
{
APIRequestList.GetVipDayGift(result =>
{
if (result != null)
{
UIManager.Instance.ShowBoardError("领取成功");
// 切换到已领取状态
btn_Award_Get.gameObject.SetActive(false);
btn_Award_GetEnd.SetActive(true);
// 刷新用户数据
var userId = GameManager.Instance?.user?.id;
if (!string.IsNullOrEmpty(userId))
APIRequestList.GetUser(userId, a => GameManager.Instance.UpdataUserData(a));
}
else
{
UIManager.Instance.ShowBoardError("领取失败");
}
});
});
}
}
private int GetVIPBGIndex(int level)
{
if (level <= 3) return 0;
if (level <= 7) return 1;
return 2;
}
private void ShowGiftItems(List<GiftConfig> gifts, Transform parent)
{
// 清空旧物品
for (int i = parent.childCount - 1; i >= 0; i--)
Destroy(parent.GetChild(i).gameObject);
if (gifts == null) return;
foreach (var gift in gifts)
{
var obj = Instantiate(btn_AwardItemPrefab.gameObject, parent);
var item = obj.GetComponent<BackPackPropItem>();
int count = gift.isRandom ? gift.minCount : gift.count;
switch (gift.type)
{
case 1: // 金币
item.Init("", count, PropEnum.none);
break;
case 2: // 钻石
item.Init("", count, PropEnum.none);
break;
case 3: // 道具
string url = gift.product != null ? APIManager.baseURL + gift.product.imageUrl : "";
PropEnum propType = (PropEnum)gift.product.type;
item.Init(url, count, propType);
break;
}
}
}
}