简体   繁体   中英

Unity3d UI Elements and Text Objects Not Updating

I am currently developing a mobile game using Unity 5 that includes a timer. 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. 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. 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.

Here you have some options:

  1. Try to use a adjust "Width" to a larger value on 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.)


References:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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