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

293 lines
9.9 KiB
C#
Raw Normal View History

2026-06-27 22:22:58 +08:00
using Assets.Framework.Tool;
using Assets.MyGame.Scripts.Net;
using Assets.MyGame.Scripts.Net.API.GameConfig_SubClass;
2025-12-11 16:40:24 +08:00
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
2025-12-26 11:15:56 +08:00
public class UIHome_BY_VIPPanel : SubUIBase
2025-12-11 16:40:24 +08:00
{
2026-06-27 21:12:04 +08:00
[Header("VIP等级-名字-介绍")]
2025-12-11 16:40:24 +08:00
public TextMeshProUGUI txt_VIPLevel;
2026-06-27 21:12:04 +08:00
public TextMeshProUGUI txt_VIPUpLeaveLabel;
public TextMeshProUGUI txt_VIPContent;
2026-02-03 11:35:19 +08:00
public TextMeshProUGUI txt_VIPEXP;
2025-12-11 16:40:24 +08:00
public Slider EXP;
2026-06-27 21:12:04 +08:00
[Header("Vip等级选项")]
public Toggle btn_LeaveItemPrefab;
public Transform btn_LeaveItemParent;
2026-07-06 04:06:56 +08:00
[Header("跳转商城")]
public Button btn_Shop;
2026-06-27 21:12:04 +08:00
[Header("Vip领取奖励选项")]
2026-06-27 22:22:58 +08:00
public BackPackPropItem btn_AwardItemPrefab;
2026-06-27 21:12:04 +08:00
public Transform btn_AwardItemParent_Top;
public Transform btn_AwardItemParent_Bottom;
public TextMeshProUGUI txt_VIPNUnlock;
public TextMeshProUGUI txt_VIPNFreeGift;
public Button btn_Award_Get;
2026-06-27 22:22:58 +08:00
public GameObject btn_Award_GetEnd;
public GameObject btn_Award_NotUnlock;
public List<GameObject> img_VIPNumBG;
private int vipLevel = 0;
2026-07-23 15:07:51 +08:00
private int currentDisplayLevel = 0; // 当前正在查看的等级(用于数据刷新后还原显示)
2026-06-27 22:22:58 +08:00
private List<VipLevelConfig> vipConfigs;
2026-02-03 11:35:19 +08:00
public override void Awake()
{
base.Awake();
2026-06-27 22:22:58 +08:00
vipConfigs = GameManager.Instance.gameConfig.vipLevelConfigs;
2026-06-27 22:35:16 +08:00
GameManager.Instance.OnUserDataChanged += OnUserDataChanged;
2026-07-06 04:06:56 +08:00
// 跳转商城(默认金币页,可传 ShopTab)
if (btn_Shop != null)
btn_Shop.onClick.AddListener(() => uIManager.ShowAsync(UIPanel.ShopPanel, ShopTab.Diamond));
2026-06-27 22:35:16 +08:00
}
private void OnUserDataChanged()
{
vipLevel = GameManager.Instance.user.assets.vipLevel;
2026-07-23 15:07:51 +08:00
// 拉取到最新 UserToken 后,刷新当前查看等级的 UI(经验、进度条、按钮状态等)
if (currentDisplayLevel > 0 && vipConfigs != null)
UpdateVIPPanel(currentDisplayLevel);
2026-06-27 22:22:58 +08:00
}
2026-02-03 11:35:19 +08:00
protected override void OnShow(object args = null)
{
2026-06-14 20:26:21 +08:00
base.OnShow(args);
2026-06-27 23:52:36 +08:00
2026-06-27 22:22:58 +08:00
vipConfigs = GameManager.Instance.gameConfig.vipLevelConfigs;
if (vipConfigs == null || vipConfigs.Count == 0) return;
2026-02-03 11:35:19 +08:00
2026-06-27 23:52:36 +08:00
vipLevel = GameManager.Instance.user.assets.vipLevel;
2026-06-27 22:22:58 +08:00
BuildLevelTabs();
2026-06-27 23:52:36 +08:00
// 直接刷新一次 UI(避免 Toggle 不触发)
UpdateVIPPanel(vipLevel);
// 再同步 Toggle 显示状态
SyncToggleState();
2026-06-27 22:22:58 +08:00
}
private void BuildLevelTabs()
{
for (int i = btn_LeaveItemParent.childCount - 1; i >= 0; i--)
2026-06-27 22:35:16 +08:00
Destroy(btn_LeaveItemParent.GetChild(i).gameObject);
2026-06-27 22:22:58 +08:00
foreach (var item in vipConfigs)
{
var toggle = Instantiate(btn_LeaveItemPrefab, btn_LeaveItemParent);
int level = item.level;
2026-06-27 23:52:36 +08:00
2026-06-27 22:22:58 +08:00
toggle.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = $"贵族{level}";
2026-06-27 23:52:36 +08:00
2026-06-27 22:22:58 +08:00
toggle.onValueChanged.RemoveAllListeners();
toggle.onValueChanged.AddListener((isOn) =>
{
2026-06-27 23:52:36 +08:00
if (isOn)
UpdateVIPPanel(level);
2026-06-27 22:22:58 +08:00
});
}
}
2026-06-27 23:52:36 +08:00
private void SyncToggleState()
{
for (int i = 0; i < btn_LeaveItemParent.childCount; i++)
{
var tog = btn_LeaveItemParent.GetChild(i).GetComponent<Toggle>();
2026-07-02 15:24:25 +08:00
if (vipConfigs[i].level== vipLevel)
{
tog.SetIsOnWithoutNotify(true);
//tog.isOn = true;
2026-06-27 23:52:36 +08:00
2026-07-02 15:24:25 +08:00
}
//bool isCurrent = vipConfigs[i].level == vipLevel;
//// 不触发事件,仅同步显示
//tog.SetIsOnWithoutNotify(isCurrent);
2026-06-27 23:52:36 +08:00
}
}
2026-06-27 22:22:58 +08:00
public void UpdateVIPPanel(int level)
{
var config = vipConfigs?.Find(v => v.level == level);
if (config == null) return;
2026-07-23 15:07:51 +08:00
currentDisplayLevel = level;
2026-06-27 22:22:58 +08:00
txt_VIPLevel.text = $"{level}";
txt_VIPContent.text = config.remark;
2026-06-27 23:52:36 +08:00
// VIP背景
2026-06-27 22:22:58 +08:00
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;
2026-07-01 18:45:43 +08:00
// 当前VIP经验
double currentExp = assets?.vipExp ?? 0;
2026-07-01 16:00:55 +08:00
// 进度条:显示选中等级的区间进度
2026-06-27 22:22:58 +08:00
EXP.minValue = config.expMin;
2026-07-01 16:00:55 +08:00
EXP.maxValue = config.expMax;
EXP.value = Mathf.Clamp((float)currentExp, config.expMin, config.expMax);
// 经验文本:当前经验 / 选中等级上限
2026-06-27 22:22:58 +08:00
txt_VIPEXP.text = $"{currentExp}/{config.expMax}";
2026-07-06 04:06:56 +08:00
// 升到下一级所需的经验 → 换成金额(1元=100经验)
double needExp = config.expMax > currentExp ? config.expMax - currentExp : 0;
double needAmount = needExp / 100.0;
2026-07-01 16:00:55 +08:00
2026-07-06 04:06:56 +08:00
int nextLevel = level + 1;
2026-07-01 16:00:55 +08:00
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 = "已满级";
}
2026-06-27 22:22:58 +08:00
txt_VIPNUnlock.text = $"贵族{level}解锁";
ShowGiftItems(config.giftList, btn_AwardItemParent_Top);
txt_VIPNFreeGift.text = $"贵族{level}免费礼包";
ShowGiftItems(config.dailyGift, btn_AwardItemParent_Bottom);
2026-07-24 01:37:11 +08:00
// 按钮状态:只在当前 vip 等级显示领取/已领取按钮,其他等级都不显示
if (level == vipLevel)
2026-06-27 22:22:58 +08:00
{
btn_Award_NotUnlock.SetActive(false);
2026-07-24 01:37:11 +08:00
bool isDayGiftReceived = assets != null && assets.isVipDayGift;
btn_Award_Get.gameObject.SetActive(!isDayGiftReceived);
btn_Award_GetEnd.SetActive(isDayGiftReceived);
2026-07-23 15:07:51 +08:00
2026-07-24 01:37:11 +08:00
if (!isDayGiftReceived)
2026-06-27 22:22:58 +08:00
{
2026-07-23 15:07:51 +08:00
btn_Award_Get.onClick.RemoveAllListeners();
btn_Award_Get.onClick.AddListener(() =>
2026-06-27 22:22:58 +08:00
{
2026-07-24 01:37:11 +08:00
APIRequestList.GetVipDayGift(result =>
2026-06-27 22:22:58 +08:00
{
2026-07-23 15:07:51 +08:00
if (result != null)
{
UIManager.Instance.ShowBoardError("领取成功");
2026-07-24 01:37:11 +08:00
// 手动设置每日礼包已领取状态,并刷新 UI
if (GameManager.Instance.user.assets != null)
2026-07-23 15:07:51 +08:00
{
2026-07-24 01:37:11 +08:00
GameManager.Instance.user.assets.isVipDayGift = true;
UpdateVIPPanel(currentDisplayLevel);
}
2026-07-23 15:07:51 +08:00
}
else
{
UIManager.Instance.ShowBoardError("领取失败");
}
});
2026-06-27 22:22:58 +08:00
});
2026-07-23 15:07:51 +08:00
}
2026-06-27 22:22:58 +08:00
}
2026-07-24 01:37:11 +08:00
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);
}
2026-06-27 22:22:58 +08:00
}
private int GetVIPBGIndex(int level)
{
if (level <= 3) return 0;
if (level <= 7) return 1;
return 2;
}
2026-07-02 15:24:25 +08:00
protected override void OnHide()
{
base.OnHide();
2026-09-02 19:57:55 +08:00
GameManager.Instance.OnUserDataChanged -= OnUserDataChanged;
2026-07-02 15:24:25 +08:00
Destroy(this.gameObject);
}
2026-09-02 19:57:55 +08:00
private void OnDestroy()
{
// 兜底:防止面板被销毁后仍留在事件里(否则之后任何 UpdataUserData 都会调到已销毁的面板)
if (GameManager.Instance != null)
GameManager.Instance.OnUserDataChanged -= OnUserDataChanged;
}
2026-06-27 22:22:58 +08:00
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>();
2026-06-27 23:52:36 +08:00
2026-06-27 22:22:58 +08:00
int count = gift.isRandom ? gift.minCount : gift.count;
switch (gift.type)
{
2026-06-27 23:52:36 +08:00
case 1:
2026-07-09 20:46:53 +08:00
item.Init(new BackPackPropItemData { count = count, giftType = 1 });
2026-06-27 22:22:58 +08:00
break;
2026-06-27 23:52:36 +08:00
case 2:
2026-07-09 20:46:53 +08:00
item.Init(new BackPackPropItemData { count = count, giftType = 2 });
2026-06-27 22:22:58 +08:00
break;
2026-06-27 23:52:36 +08:00
case 3:
2026-07-09 19:35:25 +08:00
{
2026-07-18 14:39:25 +08:00
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;
}
2026-02-03 11:35:19 +08:00
}
}
}
2025-12-11 16:40:24 +08:00
}