简体   繁体   English

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

[英]Unity3d UI Elements and Text Objects Not Updating

I am currently developing a mobile game using Unity 5 that includes a timer. 我目前正在使用带有计时器的Unity 5开发一款手机游戏。 The timer is supposed to be updating every second. 该计时器应该每秒更新一次。

I have a weird and stupid problem I cannot find the solution to. 我有一个奇怪而又愚蠢的问题,无法找到解决方案。 I'm hoping someone has encountered something like this before. 我希望有人以前遇到过这样的事情。 I initially made the timer a GUI object, before I had Unity 5. It worked correctly when it was a GUIText. 在拥有Unity 5之前,我最初使计时器成为GUI对象。当它是GUIText时,它可以正常工作。 Basically, what is happening is in the inspector, the text component changes correctly. 基本上,发生的事情是在检查器中,文本组件正确更改。 However, in realtime, in game view, the text does not change. 但是,在游戏视图中,实时文本不会更改。 I also already adapted my code from the GUIText to Text. 我也已经将我的代码从GUIText修改为Text。 So that is not the problem either. 因此,这也不是问题。 The snippets of my code are below: 我的代码段如下:

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;
}
}

Here is a screenshot of what I see on my screen: 这是我在屏幕上看到的屏幕截图: 在此处输入图片说明

According your screenshot your TextComponent is too small, so the texts are being truncated. 根据屏幕截图,您的TextComponent太小,因此文本被截断了。

Here you have some options: 在这里,您有一些选择:

  1. Try to use a adjust "Width" to a larger value on Rect Transform. 尝试在Rect Transform上使用较大的调整“宽度”。
  2. Try to use "Best Fit" option on Text Component. 尝试在文本组件上使用“最适合”选项。
  3. Try to set "Horizontal Overflow" as 'Overflow' on Text Component. 尝试在文本组件上将“水平溢出”设置为“溢出”。

I suggest you to try the first option and check what happens with some border cases values (00:00:00, 59:59:59, 100:59:59, -100:59:59, etc.. besides the final (or normal) values that will be used on the TextComponent you have to assure that even with wrong values your TextComponent will act as expected.) 我建议您尝试第一个选项,并检查某些边界条件值(00:00:00、59:59:59、100:59:59,-100:59:59等)会发生什么。除了最后一个(或正常值)将要用于TextComponent的值,您必须确保即使使用错误的值,您的TextComponent也会按预期运行。)


References: 参考文献:

http://docs.unity3d.com/es/current/Manual/script-Text.html http://docs.unity3d.com/es/current/Manual/script-Text.html

Some video tutorials: 一些视频教程:

https://unity3d.com/es/learn/tutorials/modules/beginner/ui/ui-text https://unity3d.com/es/learn/tutorials/projects/roll-a-ball/displaying-text 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM