我目前正在使用 Unity 5 开发一款包含计时器的手机游戏。计时器应该每秒更新一次。
我有一个奇怪而愚蠢的问题,我找不到解决方案。我希望有人以前遇到过这样的事情。在我拥有 Unity 5 之前,我最初将计时器设为 GUI 对象。当它是 GUIText 时,它可以正常工作。基本上,正在发生的事情是在检查器中,文本组件正确更改。然而,在实时,在游戏视图中,文本不会改变。我也已经将我的代码从 GUIText 调整为 Text。所以这也不是问题。我的代码片段如下:
private Text guiText;
void Start () {
guiText = gameObject.GetComponent<Text>();
guiText.text = timer.ToString();
}
void Update () {
timer += Time.deltaTime;
timerString = timerFormat(timer);
guiText.text = timerString;
}
string timerFormat(float currentTime)
{
TimeSpan ts = TimeSpan.FromSeconds(Mathf.Round(currentTime));
int hours = ts.Hours;
int minutes = ts.Minutes;
int seconds = ts.Seconds;
timerString = string.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
return timerString;
}
}