2015-10-04 44 views
0

我目前正在開發一款使用Unity 5的手機遊戲,其中包含一個計時器。計時器應該每秒更新一次。Unity3d UI元素和文本對象不更新

我有一個奇怪和愚蠢的問題,我找不到解決方案。我希望以前有人遇到過這樣的事情。我最初讓定時器成爲一個GUI對象,在我有Unity 5之前,它在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; 
} 
} 

這裏是什麼,我看到我的屏幕上的截圖: enter image description here

+0

你試過把一些日誌,看看是否Update方法被調用? – mayo

+0

@mayo有趣的是我知道更新方法被調用,因爲在檢查器中,我看到了對象更新的文本組件。奇怪的是,在遊戲中,用戶界面仍然保持與我最初投入的默認文本相同。 –

+0

:O奇怪! 您可以添加Inspector/Scene/Hierarchy視圖的屏幕截圖嗎? – mayo

回答

1

根據你的截圖你Choice上太小,所以文本被截斷。

在這裏,你有一些選擇:

  1. 嘗試使用調整「寬度」對矩形較大的值變換。
  2. 嘗試在文本組件上使用「最適合」選項。
  3. 嘗試將「水平溢出」設置爲文本組件上的「溢出」。

我建議你嘗試第一個選項,並檢查一些邊界案例值(00:00:00,59:59:59,100:59:59,-100:59:59等等..除了將要在TextComponent中所使用的最終(或正常)值,你必須保證,即使有錯誤的值按照預期的Choice上會採取行動)


參考文獻:

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