Files
daluandou/Assets/Script/GoldUI.cs
T
huadexuan 1cdb7c3910 ```
feat(buildings): 添加金矿建筑功能并优化建筑健康系统

- 为兵营和金矿添加伤害数字显示功能,包括持续时间、上升高度等参数
- 将金矿脚本重构为GoldMine组件,实现定时产出金币机制
- 为所有单位添加NavMeshAgent组件以支持导航寻路
- 为弓箭手、法师等单位添加分离半径和目标扩散参数
- 为建筑添加碰撞体和标签设置
- 修复当前能量字段命名规范(currentEnergy -> _currentEnergy)
- 调整金矿健康值从100提升至500
- 统一伤害数字显示相关属性配置
```
2026-09-15 21:13:44 +08:00

58 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
/// <summary>
/// 金币 UI 显示,显示在屏幕右上角
/// </summary>
[RequireComponent(typeof(Text))]
public class GoldUI : MonoBehaviour
{
private Text goldText;
private bool isSubscribed = false;
private void Awake()
{
goldText = GetComponent<Text>();
}
private void Start()
{
// 延迟订阅,确保 GoldManager 已初始化
StartCoroutine(SubscribeToGoldManager());
}
private IEnumerator SubscribeToGoldManager()
{
// 等待 GoldManager 初始化
while (GoldManager.Instance == null)
{
yield return null;
}
// 订阅事件
GoldManager.Instance.OnGoldChanged += UpdateGoldDisplay;
isSubscribed = true;
// 立即更新显示
UpdateGoldDisplay(GoldManager.Instance.CurrentGold);
}
private void OnDestroy()
{
// 取消订阅
if (isSubscribed && GoldManager.Instance != null)
{
GoldManager.Instance.OnGoldChanged -= UpdateGoldDisplay;
}
}
private void UpdateGoldDisplay(int newGold)
{
if (goldText != null)
{
goldText.text = $"金币: {newGold}";
}
}
}