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

标签 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 上。

error CS1061: Type 'UnityEngine.GameObject' does not contain a definition for 'GUIText' and no extension method 'GUIText' of type 'UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

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

最佳答案

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

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

关于c# - Unity 5 中的 GUIText(需要替代方案),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714357/

相关文章:

c# - 如何访问以前添加到委托(delegate)的函数

c# - 将 SqlDataReader 传递给多个线程时是否应该通过引用传递 SqlDataReader

unity3d - Daydream Controller 无法与 Canvas 按钮正确交互

c# - 为什么在unity中访问另一个类变量时需要attach或reference and object?

.net - 在导入/导出时具有 Markdown 功能的 GUI 控制

C#:如何将 RAW 图像(格式:rgb565)加载到位图中?

c# - ObjectListView 不自动换行

android - Cardboard sdk 右场景和左场景有错误?

javascript - jQuery - 获取包含 HTML 标签的文本区域的值

ruby - 使用 Ruby 和 Pandoc 将 Markdown 文本转换为 RTF?