100 lines
3.4 KiB
C#
Raw Permalink Normal View History

2025-09-19 19:43:49 +05:00
using Cysharp.Threading.Tasks;
using System;
using System.Threading;
2025-09-19 14:56:58 +05:00
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BulletHellTemplate
{
public class CurrencyDisplay : MonoBehaviour
{
[Header("Currency Settings")]
public string currencyID;
public TextMeshProUGUI currencyAmount;
public Image currencyIcon;
2025-09-19 19:43:49 +05:00
[Header("Recharge UI")]
2025-09-19 14:56:58 +05:00
public bool isRechargeable;
public TextMeshProUGUI rechargeAmount;
public TextMeshProUGUI nextRechargeTime;
2025-09-19 19:43:49 +05:00
private Currency curData;
private CancellationTokenSource cts;
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
/*────────────────── Unity ──────────────────*/
2025-09-19 14:56:58 +05:00
private void OnEnable()
{
MonetizationManager.OnCurrencyChanged += UpdateCurrencyDisplay;
2025-09-19 19:43:49 +05:00
cts = new CancellationTokenSource();
InitAsync(cts.Token).Forget();
2025-09-19 14:56:58 +05:00
}
private void OnDisable()
{
MonetizationManager.OnCurrencyChanged -= UpdateCurrencyDisplay;
2025-09-19 19:43:49 +05:00
cts?.Cancel();
cts = null;
2025-09-19 14:56:58 +05:00
}
2025-09-19 19:43:49 +05:00
/*────────────────── Init ──────────────────*/
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
private async UniTaskVoid InitAsync(CancellationToken token)
2025-09-19 14:56:58 +05:00
{
2025-09-19 19:43:49 +05:00
await BackendManager.CheckInitialized();
await UniTask.WaitUntil(() =>
GameInstance.Singleton != null &&
GameInstance.Singleton.currencyData != null &&
MonetizationManager.Singleton != null);
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
curData = GameInstance.Singleton.currencyData.Find(c => c.coinID == currencyID);
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
if (curData != null && currencyIcon != null)
currencyIcon.sprite = curData.icon;
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
UpdateCurrencyDisplay(currencyID, MonetizationManager.GetCurrency(currencyID));
UpdateRechargeUI();
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
while (!token.IsCancellationRequested &&
isRechargeable &&
curData?.isRechargeableCurrency == true)
2025-09-19 14:56:58 +05:00
{
2025-09-19 19:43:49 +05:00
UpdateRechargeUI();
await UniTask.Delay(1000, cancellationToken: token);
2025-09-19 14:56:58 +05:00
}
}
2025-09-19 19:43:49 +05:00
/*────────────────── UI helpers ──────────────────*/
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
private void UpdateCurrencyDisplay(string changed, int amount)
2025-09-19 14:56:58 +05:00
{
2025-09-19 19:43:49 +05:00
if (changed == currencyID && currencyAmount != null)
2025-09-19 14:56:58 +05:00
currencyAmount.text = amount.ToString();
}
2025-09-19 19:43:49 +05:00
private void UpdateRechargeUI()
2025-09-19 14:56:58 +05:00
{
2025-09-19 19:43:49 +05:00
if (!isRechargeable || curData == null)
2025-09-19 14:56:58 +05:00
{
if (rechargeAmount) rechargeAmount.gameObject.SetActive(false);
if (nextRechargeTime) nextRechargeTime.gameObject.SetActive(false);
return;
}
if (rechargeAmount) rechargeAmount.gameObject.SetActive(true);
if (nextRechargeTime) nextRechargeTime.gameObject.SetActive(true);
2025-09-19 19:43:49 +05:00
OfflineRechargeHandler.TryGetSecondsToNextTick(
currencyID, out float seconds, out bool atMax);
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
if (rechargeAmount)
rechargeAmount.text = atMax ? "" : $"+{curData.rechargeAmount}";
2025-09-19 14:56:58 +05:00
if (nextRechargeTime)
2025-09-19 19:43:49 +05:00
nextRechargeTime.text = atMax ? "" : $"{Mathf.CeilToInt(seconds)}s";
2025-09-19 14:56:58 +05:00
}
}
}