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 img_VIPNumBG; private int vipLevel = 0; private List 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; } 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().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(); 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; 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}"; // 升到下一级所需的金额 double needAmount = config.expMax > currentExp ? config.expMax - currentExp : 0; int nextLevel = vipLevel + 1; bool hasNextLevel = vipConfigs.Exists(v => v.level == nextLevel); if (needAmount > 0 && hasNextLevel) { txt_VIPUpLeaveLabel.text = $"再充值 {needAmount} 元即可升级至贵族{nextLevel}"; } else { txt_VIPUpLeaveLabel.text = "已满级"; } 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; } protected override void OnHide() { base.OnHide(); Destroy(this.gameObject); } private void ShowGiftItems(List 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(); 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; } } } }