Files
5.24BuYu/Assets/MyGame/aa/UI/Home/Sub/UIHome_BY_VIPPanel.cs
T
2026-09-02 19:57:55 +08:00

293 lines
9.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("跳转商城")]
public Button btn_Shop;
[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 int currentDisplayLevel = 0; // 当前正在查看的等级(用于数据刷新后还原显示)
private List<VipLevelConfig> vipConfigs;
public override void Awake()
{
base.Awake();
vipConfigs = GameManager.Instance.gameConfig.vipLevelConfigs;
GameManager.Instance.OnUserDataChanged += OnUserDataChanged;
// 跳转商城(默认金币页,可传 ShopTab)
if (btn_Shop != null)
btn_Shop.onClick.AddListener(() => uIManager.ShowAsync(UIPanel.ShopPanel, ShopTab.Diamond));
}
private void OnUserDataChanged()
{
vipLevel = GameManager.Instance.user.assets.vipLevel;
// 拉取到最新 UserToken 后,刷新当前查看等级的 UI(经验、进度条、按钮状态等)
if (currentDisplayLevel > 0 && vipConfigs != null)
UpdateVIPPanel(currentDisplayLevel);
}
protected override void OnShow(object args = null)
{
base.OnShow(args);
vipConfigs = GameManager.Instance.gameConfig.vipLevelConfigs;
if (vipConfigs == null || vipConfigs.Count == 0) return;
vipLevel = GameManager.Instance.user.assets.vipLevel;
BuildLevelTabs();
// 直接刷新一次 UI(避免 Toggle 不触发)
UpdateVIPPanel(vipLevel);
// 再同步 Toggle 显示状态
SyncToggleState();
}
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;
toggle.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = $"贵族{level}";
toggle.onValueChanged.RemoveAllListeners();
toggle.onValueChanged.AddListener((isOn) =>
{
if (isOn)
UpdateVIPPanel(level);
});
}
}
private void SyncToggleState()
{
for (int i = 0; i < btn_LeaveItemParent.childCount; i++)
{
var tog = btn_LeaveItemParent.GetChild(i).GetComponent<Toggle>();
if (vipConfigs[i].level== vipLevel)
{
tog.SetIsOnWithoutNotify(true);
//tog.isOn = true;
}
//bool isCurrent = vipConfigs[i].level == vipLevel;
//// 不触发事件,仅同步显示
//tog.SetIsOnWithoutNotify(isCurrent);
}
}
public void UpdateVIPPanel(int level)
{
var config = vipConfigs?.Find(v => v.level == level);
if (config == null) return;
currentDisplayLevel = level;
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;
// 当前VIP经验
double currentExp = assets?.vipExp ?? 0;
// 进度条:显示选中等级的区间进度
EXP.minValue = config.expMin;
EXP.maxValue = config.expMax;
EXP.value = Mathf.Clamp((float)currentExp, config.expMin, config.expMax);
// 经验文本:当前经验 / 选中等级上限
txt_VIPEXP.text = $"{currentExp}/{config.expMax}";
// 升到下一级所需的经验 → 换成金额(1元=100经验)
double needExp = config.expMax > currentExp ? config.expMax - currentExp : 0;
double needAmount = needExp / 100.0;
int nextLevel = level + 1;
bool hasNextLevel = vipConfigs.Exists(v => v.level == nextLevel);
if (needAmount > 0 && hasNextLevel)
{
txt_VIPUpLeaveLabel.text =
$"再充值 <color=yellow>{needAmount}</color> 元即可升级至<color=yellow><size=36>贵族{nextLevel}</size></color>";
}
else
{
txt_VIPUpLeaveLabel.text = "已满级";
}
txt_VIPNUnlock.text = $"贵族{level}解锁";
ShowGiftItems(config.giftList, btn_AwardItemParent_Top);
txt_VIPNFreeGift.text = $"贵族{level}免费礼包";
ShowGiftItems(config.dailyGift, btn_AwardItemParent_Bottom);
// 按钮状态:只在当前 vip 等级显示领取/已领取按钮,其他等级都不显示
if (level == vipLevel)
{
btn_Award_NotUnlock.SetActive(false);
bool isDayGiftReceived = assets != null && assets.isVipDayGift;
btn_Award_Get.gameObject.SetActive(!isDayGiftReceived);
btn_Award_GetEnd.SetActive(isDayGiftReceived);
if (!isDayGiftReceived)
{
btn_Award_Get.onClick.RemoveAllListeners();
btn_Award_Get.onClick.AddListener(() =>
{
APIRequestList.GetVipDayGift(result =>
{
if (result != null)
{
UIManager.Instance.ShowBoardError("领取成功");
// 手动设置每日礼包已领取状态,并刷新 UI
if (GameManager.Instance.user.assets != null)
{
GameManager.Instance.user.assets.isVipDayGift = true;
UpdateVIPPanel(currentDisplayLevel);
}
}
else
{
UIManager.Instance.ShowBoardError("领取失败");
}
});
});
}
}
else 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(false);
btn_Award_GetEnd.SetActive(false);
btn_Award_NotUnlock.SetActive(false);
}
}
private int GetVIPBGIndex(int level)
{
if (level <= 3) return 0;
if (level <= 7) return 1;
return 2;
}
protected override void OnHide()
{
base.OnHide();
GameManager.Instance.OnUserDataChanged -= OnUserDataChanged;
Destroy(this.gameObject);
}
private void OnDestroy()
{
// 兜底:防止面板被销毁后仍留在事件里(否则之后任何 UpdataUserData 都会调到已销毁的面板)
if (GameManager.Instance != null)
GameManager.Instance.OnUserDataChanged -= OnUserDataChanged;
}
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(new BackPackPropItemData { count = count, giftType = 1 });
break;
case 2:
item.Init(new BackPackPropItemData { count = count, giftType = 2 });
break;
case 3:
{
string url = gift.product?.imageUrl;
PropEnum propEnum = PropEnum.none;
if (gift.product != null)
{
url = gift.product.imageUrl;
propEnum = (PropEnum)gift.product.type;
}
else if (!string.IsNullOrEmpty(gift.otherId))
{
var match = GameManager.Instance?.gameConfig?.propConfig?.Find(p => p.number == gift.otherId);
if (match != null)
{
url = match.imageUrl;
propEnum = (PropEnum)match.type;
}
}
item.Init(new BackPackPropItemData
{
url = !string.IsNullOrEmpty(url) ? APIManager.baseURL + url : "",
count = count,
giftType = 3,
propEnum = propEnum
});
break;
}
}
}
}
}