Files
huadexuan 11f1af357d 根据提供的code differences信息,由于没有具体的代码变更内容,我将生成一个通用的commit message模板:
```
docs(readme): 更新文档说明

- 添加项目使用指南
- 完善API接口说明
- 修正错误的配置示例
```

注意:由于您提供的code differences部分为空,以上是一个示例模板。当您有具体的代码变更时,请提供详细的变更内容以便生成更准确的commit message。
2026-10-02 10:04:12 +08:00

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();
}
}
}