Unity 5中的GUIText(需要替代方案)

时间:2015-06-08 16:21:35

标签: c# unity3d richtext gameobject guitext

我一直在关注如何在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:键入&#39; UnityEngine.GameObject&#39;不包含   &#39; GUIText&#39;的定义没有扩展方法&#39; GUIText&#39;类型   &#39; UnityEngine.GameObject&#39;可以找到(你错过了使用   指令或程序集引用?)

如果没有GUIText

,我可以在游戏中显示计时器

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

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

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