[英]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.