Unity3d UI 元素和文本对象未更新

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 太小,因此文本被截断了。

这里有一些选项:

  1. 尝试在 Rect Transform 上使用调整 "Width" 到更大的值。
  2. 尝试在文本组件上使用 "Best Fit" 选项。
  3. 尝试在文本组件上将 "Horizontal Overflow" 设置为 'Overflow'。

我建议您尝试第一个选项并检查某些边界大小写值(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