using UnityEngine;
using UnityEngine.UI;
using Architecture.Core;
namespace GameFramework
{
///
/// 得分显示组件。
/// 订阅 ScoreManager.onScoreChanged 事件,自动更新 Text。
/// 挂到场景中带有 Text 组件的 Canvas 元素上即可。
///
public class ScoreDisplay : MonoBehaviour
{
[SerializeField] Text scoreText;
[SerializeField] string format = "{0}";
[Header("SO 事件通道(替代 static ScoreManager.onScoreChanged 事件)")]
[SerializeField] private IntEvent scoreChangedEvent;
void OnEnable()
{
scoreChangedEvent?.Register(UpdateText);
}
void OnDisable()
{
scoreChangedEvent?.Unregister(UpdateText);
}
void Start()
{
if (scoreText == null)
scoreText = GetComponent();
// 初始值走事件:订阅后首个 ScoreChanged 会刷新显示;分数初始即为 0,先显示 0。
UpdateText(0);
}
void UpdateText(int score)
{
if (scoreText != null)
scoreText.text = string.Format(format, score);
}
}
}