using Core.Economy;
using TMPro;
using TowerDefense.Level;
using UnityEngine;
namespace TowerDefense.UI.HUD
{
///
/// A class for controlling the displaying the currency
///
public class EndlessCurrencyUI : MonoBehaviour
{
///
/// TextMeshPro对应的金币数据
///
public TextMeshProUGUI DisplayText;
protected Currency currency;
private void Start()
{
if (EndlessLevelManager.instanceExists)
{
currency = EndlessLevelManager.instance.Currency;
UpdateDisplay();
currency.currencyChanged += UpdateDisplay;
}
else
Debug.LogError("[UI] No EndlessLevelManager to get currency from");
}
///
/// Unsubscribe from events
///
protected virtual void OnDestroy()
{
if (currency != null)
currency.currencyChanged -= UpdateDisplay;
}
///
/// A method for updating the display based on the current currency
///
protected void UpdateDisplay()
{
int current = currency.currentCurrency;
//DisplayText.text = AddNumberSemi(current.ToString());
DisplayText.text = current.ToString();
}
///
/// 在数字中加入逗号
///
///
protected string AddNumberSemi(string str)
{
int len = str.Length;
for (int ipos = len - 3; ipos > 0; ipos -= 3)
{
str = str.Insert(ipos, ",");
}
return str;
}
}
}