61 lines
1.1 KiB
C#
61 lines
1.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class GoldSystem : MonoBehaviour
|
|
{
|
|
public static GoldSystem Instance { get; private set; }
|
|
|
|
[Header("金币配置")]
|
|
[Min(0)] public int startingGold = 200;
|
|
[Min(0)] public int maxGold = 9999;
|
|
|
|
[Header("UI(可选)")]
|
|
public Text goldText;
|
|
|
|
public int CurrentGold { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
CurrentGold = startingGold;
|
|
UpdateUI();
|
|
}
|
|
|
|
public bool TrySpend(int amount)
|
|
{
|
|
if (amount < 0 || CurrentGold < amount)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
CurrentGold -= amount;
|
|
UpdateUI();
|
|
return true;
|
|
}
|
|
|
|
public void AddGold(int amount)
|
|
{
|
|
if (amount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
CurrentGold = Mathf.Min(CurrentGold + amount, maxGold);
|
|
UpdateUI();
|
|
}
|
|
|
|
private void UpdateUI()
|
|
{
|
|
if (goldText != null)
|
|
{
|
|
goldText.text = CurrentGold.ToString();
|
|
}
|
|
}
|
|
}
|