简体   繁体   English

碰撞C#上的Unity GUIText

[英]Unity GUIText on Collision c#

I'm writing a 3D maze program in C# and I need to have UI Text display "You Win!" 我正在用C#编写3D迷宫程序,我需要让UI文本显示为“ You Win!”。 When the player reaches the end of the maze. 当玩家到达迷宫尽头时。

I have a trigger set up in Unity as a cube, named FinishLine, and I have the UI text named winText 我在Unity中将触发器设置为一个名为FinishLine的多维数据集,并且将UI文本命名为winText

I'm getting an error on this line.. 我在这条线上出现错误。

GUI.Box(New rect (10,10,100,90), winText); GUI.Box(新矩形(10,10,100,90),winText);

the error is "The best overloaded method matfch for unityengine.gui.box (unityEngine rect, string)' has some invalid arguments 错误是“ Unityengine.gui.box(unityEngine rect,字符串)的最佳重载方法matfch”具有一些无效的参数

I also have no idea what those numbers are (10,10,100,90), so maybe that's messing something up? 我也不知道这些数字是多少(10,10,100,90),所以也许这搞砸了吗? What are those values indicating...? 这些值表示什么?

Here is my code.. 这是我的代码。

public class TextTrigger : MonoBehaviour {

     public GUIText winText;
     private bool FinishLine = false;

     void Start () {
         FinishLine = false;
     }

     void OnTriggerEnter(Collider col){
         if (col.tag == "Player") {
             FinishLine = true;   
         }
     }

     void OnGui() {
         GUI.Box(new Rect(10,10,100,90), winText);
     }
 }

EDIT - Updated my code, and I have a new error. 编辑-更新了我的代码,并且出现了新错误。 On line 21 it says: "UnityEngine.Texture does not contain a definition for text and no extension method 'text' accepting a first argument of type 'UnityEngine.Texture' could be found. Are you missing a using directive or an assembly refrence? 在第21行上说: “ UnityEngine.Texture不包含文本的定义,找不到可以接受类型为'UnityEngine.Texture'的第一个参数的扩展方法'text'。您是否缺少using指令或程序集引用?

NEW CODE: 新代码:

using System.Collections; 使用System.Collections; using System.Collections.Generic; 使用System.Collections.Generic; using UnityEngine; 使用UnityEngine; using UnityEngine.UI; 使用UnityEngine.UI;

public class FinishLine : MonoBehaviour { 公共类FinishLine:MonoBehaviour {

 public Texture winText; private bool FinishPlane = false; // Use this for initialization void Start () { FinishPlane = 

false; 假;

 } void OnTriggerEnter(Collider col) { if (col.tag == "Player") { FinishPlane = true; winText.text = "You Win!"; } } } 

First of all, it is OnGUI not OnGui . 首先,它是OnGUI而不是OnGui The spelling counts. 拼写很重要。 If you find yourself using OnGUI , stop and find other ways to accomplish whatever you are doing. 如果您发现自己使用OnGUI ,请停止并找到其他方式来完成您的工作。

GUIText is a legacy UI Component. GUIText是旧版UI组件。 It's old and the Text component should now be used. 它很旧,现在应该使用Text组件。 If you still want to use it, below is the proper way to use GUIText . 如果仍要使用它,则下面是使用GUIText的正确方法。

public GUIText winText;
private bool FinishLine = false;

void Start()
{
    FinishLine = false;
}

void OnTriggerEnter(Collider col)
{
    if (col.tag == "Player")
    {
        FinishLine = true;
        winText.text = "You Win";
    }
}

Text component should be used for this and below is how to do that with the Text component: 为此,应使用Text组件,下面是如何使用Text组件做到这一点:

public Text winText;
private bool FinishLine = false;

void Start()
{
    FinishLine = false;
}

void OnTriggerEnter(Collider col)
{
    if (col.tag == "Player")
    {
        FinishLine = true;
        winText.text = "You Win";
    }
}

You can learn more about Unity's new UI here . 您可以在此处了解有关Unity新UI的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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