重构
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using Architecture.Core;
|
||||
|
||||
namespace GameFramework
|
||||
{
|
||||
@@ -13,24 +14,25 @@ namespace GameFramework
|
||||
[SerializeField] Text scoreText;
|
||||
[SerializeField] string format = "{0}";
|
||||
|
||||
[Header("SO 事件通道(替代 static ScoreManager.onScoreChanged 事件)")]
|
||||
[SerializeField] private IntEvent scoreChangedEvent;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
ScoreManager.onScoreChanged += UpdateText;
|
||||
scoreChangedEvent?.Register(UpdateText);
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
ScoreManager.onScoreChanged -= UpdateText;
|
||||
scoreChangedEvent?.Unregister(UpdateText);
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (scoreText == null)
|
||||
scoreText = GetComponent<Text>();
|
||||
if (ScoreManager.Instance != null)
|
||||
UpdateText(ScoreManager.Instance.CurrentScore);
|
||||
else
|
||||
UpdateText(0);
|
||||
// 初始值走事件:订阅后首个 ScoreChanged 会刷新显示;分数初始即为 0,先显示 0。
|
||||
UpdateText(0);
|
||||
}
|
||||
|
||||
void UpdateText(int score)
|
||||
|
||||
Reference in New Issue
Block a user