冲突c#上的Unity GUIText [英] Unity GUIText on Collision c#
问题描述
我正在用C#编写3D迷宫程序,我需要让UI文本显示为"You Win!".当玩家到达迷宫尽头时.
我在Unity中将触发器设置为一个名为FinishLine的多维数据集,并且我的UI文本名为winText
我在这条线上出现错误.
GUI.Box(新矩形(10,10,100,90),winText);
错误是"unityengine.gui.box(unityEngine rect,字符串)的最佳重载方法matfch'具有一些无效的参数
我也不知道这些数字是多少(10,10,100,90),所以也许这使事情变得混乱了吗?那些表示...的值是什么?
这是我的代码.
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);
}
}
编辑-更新了我的代码,并且出现了新错误. 在第21行上说:"UnityEngine.Texture不包含文本定义,也找不到扩展方法'text'接受类型为'UnityEngine.Texture'的第一个参数.您是否缺少using指令或装配体反射?
新代码:
使用System.Collections;使用System.Collections.Generic;使用 UnityEngine;使用UnityEngine.UI;
公共类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!"; } } }
首先,它是OnGUI
而不是OnGui
.拼写很重要.如果您发现自己使用OnGUI
,请停下来寻找其他方法来完成您正在做的事情.
GUIText
是旧版UI组件.它很旧,现在应该使用Text
组件.如果您仍要使用它,则下面是使用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
组件应用于此目的,下面是使用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";
}
}
您可以在此处进一步了解./p>
I'm writing a 3D maze program in C# and I need to have UI Text display "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
I'm getting an error on this line..
GUI.Box(New rect (10,10,100,90), winText);
the error is "The best overloaded method matfch for unityengine.gui.box (unityEngine rect, string)' has some invalid arguments
I also have no idea what those numbers are (10,10,100,90), so maybe that's messing something up? 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?
NEW CODE:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class 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
. The spelling counts. If you find yourself using OnGUI
, stop and find other ways to accomplish whatever you are doing.
GUIText
is a legacy UI Component. It's old and the Text
component should now be used. If you still want to use it, below is the proper way to use 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:
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.
这篇关于冲突c#上的Unity GUIText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!