[英]Unity3d UI Elements and Text Objects Not Updating
我目前正在使用帶有計時器的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;
}
}
根據屏幕截圖,您的TextComponent太小,因此文本被截斷了。
在這里,您有一些選擇:
我建議您嘗試第一個選項,並檢查某些邊界條件值(00:00:00、59:59:59、100:59:59,-100:59:59等)會發生什么。除了最后一個(或正常值)將要用於TextComponent的值,您必須確保即使使用錯誤的值,您的TextComponent也會按預期運行。)
http://docs.unity3d.com/es/current/Manual/script-Text.html
一些視頻教程:
https://unity3d.com/es/learn/tutorials/modules/beginner/ui/ui-text https://unity3d.com/es/learn/tutorials/projects/roll-a-ball/displaying-text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.