``` docs(readme): 更新文档说明 - 添加项目使用指南 - 完善API接口说明 - 修正错误的配置示例 ``` 注意:由于您提供的code differences部分为空,以上是一个示例模板。当您有具体的代码变更时,请提供详细的变更内容以便生成更准确的commit message。
85 lines
2.0 KiB
C#
85 lines
2.0 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
/// <summary>
|
|
/// 金币管理器,全局单例
|
|
/// </summary>
|
|
public class GoldManager : MonoBehaviour
|
|
{
|
|
public static GoldManager Instance { get; private set; }
|
|
|
|
[Header("金币设置")]
|
|
[SerializeField] private int initialGold = 500;
|
|
[SerializeField] private int currentGold = 500;
|
|
|
|
/// <summary>
|
|
/// 当前金币数量(只读)
|
|
/// </summary>
|
|
public int CurrentGold => currentGold;
|
|
|
|
/// <summary>
|
|
/// 金币变化事件(参数:新的金币数量)
|
|
/// </summary>
|
|
public event Action<int> OnGoldChanged;
|
|
|
|
private void Awake()
|
|
{
|
|
// 单例模式
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
Instance = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
currentGold = initialGold;
|
|
OnGoldChanged?.Invoke(currentGold);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 增加金币
|
|
/// </summary>
|
|
public void AddGold(int amount)
|
|
{
|
|
if (amount <= 0) return;
|
|
|
|
currentGold += amount;
|
|
OnGoldChanged?.Invoke(currentGold);
|
|
Debug.Log($"获得金币 +{amount},当前金币: {currentGold}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 尝试消费金币,如果金币足够则扣除并返回 true,否则返回 false
|
|
/// </summary>
|
|
public bool TrySpendGold(int amount)
|
|
{
|
|
if (amount <= 0)
|
|
{
|
|
Debug.LogWarning("消费金币数量必须大于0");
|
|
return false;
|
|
}
|
|
|
|
if (currentGold < amount)
|
|
{
|
|
Debug.LogWarning($"金币不足!需要 {amount},当前只有 {currentGold}");
|
|
return false;
|
|
}
|
|
|
|
currentGold -= amount;
|
|
OnGoldChanged?.Invoke(currentGold);
|
|
Debug.Log($"消费金币 -{amount},当前金币: {currentGold}");
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查是否有足够的金币
|
|
/// </summary>
|
|
public bool HasEnoughGold(int amount)
|
|
{
|
|
return currentGold >= amount;
|
|
}
|
|
}
|