【问题标题】:GUIText in Unity 5 (Need an alternative)Unity 5 中的 GUIText(需要替代方案)
【发布时间】:2015-06-08 16:21:35
【问题描述】:

我一直在关注如何在 YouTube 上制作计时器的教程,但由于 Unity 5 中的 GUIText 问题,我现在陷入困境。这是我的代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Timer : MonoBehaviour {

    public float Seconds = 59;
    public float Minutes = 0;

    void Update() {

        if (Seconds <= 0) {

            Seconds = 59;
            if (Minutes > 1) {

                Minutes--;

            } else {

                Minutes = 0;
                Seconds = 0;
                //This makes the guiText show the time as X:XX. ToString.("f0") formats it so there is no decimal place.
                GameObject.Find("TimerText").GUIText.text = Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");

            }

        } else {

            Seconds -= Time.deltaTime;

        }

        //These lines will make sure that the time is shown as X:XX and not X:XX.XXXXXX
        if(Mathf.Round(Seconds) <= 9) {

            GameObject.Find("TimerText").GUIText.text = Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");

        } else {

            GameObject.Find("TimerText").GUIText.text = Minutes.ToString("f0") + ":" + Seconds.ToString("f0");

        }

    }

}

问题在于GUIText

错误 CS1061:类型“UnityEngine.GameObject”不包含 'GUIText' 的定义并且没有扩展方法'GUIText' 类型 可以找到“UnityEngine.GameObject”(您是否缺少使用 指令还是程序集引用?)

如果没有GUIText,我该怎么做才能在我的游戏中显示计时器?

【问题讨论】:

标签: c# unity3d richtext gameobject guitext


【解决方案1】:

不用GameObject.Find("TimerText").GUIText.text,你可以用GetComponent(string)的方法重写,这是正确的方法:

GameObject.Find("TimerText").GetComponent<GUIText>().text = 
    Minutes.ToString("f0") + ":0" + Seconds.ToString("f0");

【讨论】:

  • 谢谢!现在我没有错误了! :) 只需要弄清楚如何让“GUI 文本”中的文本可见。
【解决方案2】:

只需选择您分配此代码的游戏对象,然后在Inspector 视图中单击Add Component 并将GUI Text 组件添加到它。
不要忘记为其设置字体

【讨论】:

  • 无法让 GUI 文本组件与任何文本一起显示。
  • 确保将字体设置为某个值且字体大小不是 0
  • 我试过了,但我认为可能还有其他事情会破坏它。不管怎样,谢谢! :)
猜你喜欢
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多