2026-01-14 12:13:44 +08:00
|
|
|
|
using Framework;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using TMPro;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 控制高级账户剩余天数UI的显示和更新
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PremiumRemainingDaysUI : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
[Header("显示剩余天数的文本组件")]
|
|
|
|
|
|
[SerializeField] private TextMeshProUGUI remainingDaysText;
|
|
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
|
{
|
|
|
|
|
|
EventManager.Instance.Register<PremiumStatusChangedEvent>(EventManager.EventName.PremiumStatusRefresh, OnPremiumStatusChanged);
|
|
|
|
|
|
EventManager.Instance.Send(EventManager.EventName.RequestPremiumStatus);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
|
{
|
|
|
|
|
|
EventManager.Instance.Unregister<PremiumStatusChangedEvent>(EventManager.EventName.PremiumStatusRefresh, OnPremiumStatusChanged);
|
|
|
|
|
|
CancelInvoke(nameof(UpdateDaily));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnPremiumStatusChanged(PremiumStatusChangedEvent eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (eventData.IsActivated && eventData.ExpirationTimestamp > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
gameObject.transform.localScale = Vector3.one;
|
2026-01-16 19:35:52 +08:00
|
|
|
|
UpdateRemainingDays(eventData.RemainingSeconds);
|
2026-01-14 12:13:44 +08:00
|
|
|
|
|
|
|
|
|
|
// 为了防止重复调用,先取消之前的
|
|
|
|
|
|
CancelInvoke(nameof(UpdateDaily));
|
|
|
|
|
|
|
|
|
|
|
|
// 安排一个每日更新任务,在第二天的零点过1秒后触发
|
|
|
|
|
|
DateTime now = DateTime.Now;
|
|
|
|
|
|
DateTime tomorrow = now.AddDays(1).Date;
|
|
|
|
|
|
double secondsUntilTomorrow = (tomorrow - now).TotalSeconds;
|
|
|
|
|
|
Invoke(nameof(UpdateDaily), (float)secondsUntilTomorrow + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
gameObject.transform.localScale = Vector3.zero;
|
|
|
|
|
|
CancelInvoke(nameof(UpdateDaily));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 每日自动更新的函数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void UpdateDaily()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 重新请求一下最新状态,保证数据准确性,并重新安排下一次更新
|
|
|
|
|
|
EventManager.Instance.Send(EventManager.EventName.RequestPremiumStatus);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-01-16 19:35:52 +08:00
|
|
|
|
/// 根据剩余时间(秒)更新UI
|
2026-01-14 12:13:44 +08:00
|
|
|
|
/// </summary>
|
2026-01-16 19:35:52 +08:00
|
|
|
|
private void UpdateRemainingDays(long remainingSeconds)
|
2026-01-14 12:13:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (remainingDaysText == null) return;
|
|
|
|
|
|
|
2026-01-16 19:35:52 +08:00
|
|
|
|
TimeSpan remainingTime = TimeSpan.FromSeconds(remainingSeconds);
|
2026-01-14 12:13:44 +08:00
|
|
|
|
|
|
|
|
|
|
// 向上取整,例如剩余1.1天,也算作2天。如果小于0则为0。
|
|
|
|
|
|
int days = Math.Max(0, (int)Math.Ceiling(remainingTime.TotalDays));
|
|
|
|
|
|
|
2026-01-14 18:37:36 +08:00
|
|
|
|
int hours = remainingTime.Hours;
|
|
|
|
|
|
if (days > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
remainingDaysText.text = StringManager.Instance.GetLocalizeTextByKeyAndParams("PremiumPerk_Remain_Day", days);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
remainingDaysText.text = StringManager.Instance.GetLocalizeTextByKeyAndParams("PremiumPerk_Remain_Hour", hours);
|
|
|
|
|
|
}
|
2026-01-14 12:13:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|