43 lines
1.0 KiB
C#
43 lines
1.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace GameFramework
|
|
{
|
|
/// <summary>
|
|
/// 得分显示组件。
|
|
/// 订阅 ScoreManager.onScoreChanged 事件,自动更新 Text。
|
|
/// 挂到场景中带有 Text 组件的 Canvas 元素上即可。
|
|
/// </summary>
|
|
public class ScoreDisplay : MonoBehaviour
|
|
{
|
|
[SerializeField] Text scoreText;
|
|
[SerializeField] string format = "{0}";
|
|
|
|
void OnEnable()
|
|
{
|
|
ScoreManager.onScoreChanged += UpdateText;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
ScoreManager.onScoreChanged -= UpdateText;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if (scoreText == null)
|
|
scoreText = GetComponent<Text>();
|
|
if (ScoreManager.Instance != null)
|
|
UpdateText(ScoreManager.Instance.CurrentScore);
|
|
else
|
|
UpdateText(0);
|
|
}
|
|
|
|
void UpdateText(int score)
|
|
{
|
|
if (scoreText != null)
|
|
scoreText.text = string.Format(format, score);
|
|
}
|
|
}
|
|
}
|