C# 文本变量未结转至IF语句
我想知道为什么我的completeText变量不会包含在我用于GUI标签的IF语句中,当用户悬停在不同的模型上时,这些标签会出现。我已使用StreamReader成功导入文本,但当我放入:C# 文本变量未结转至IF语句,c#,unity3d,C#,Unity3d,我想知道为什么我的completeText变量不会包含在我用于GUI标签的IF语句中,当用户悬停在不同的模型上时,这些标签会出现。我已使用StreamReader成功导入文本,但当我放入: GUI.contentColor = Color.red; GUI.Label(new Rect(1000, 50, 400, 400), completeText); 在IF语句中没有显示任何内容。任何帮助都将不胜感激 public class OnClic
GUI.contentColor = Color.red;
GUI.Label(new Rect(1000, 50, 400, 400), completeText);
在IF语句中没有显示任何内容。任何帮助都将不胜感激
public class OnClick : MonoBehaviour
{
public bool isClicked = false;
string cdrw = "CD-RW";
string powerSupply = "Power Supply";
string hardDriveName = "Hard Drive";
string crosspeiceName = "Crosspeice";
string fanName = "Fan";
string graphicsName = "Graphics Card";
string ramName = "Ram";
public string txt;
public string completeText = "";
public GameObject cdrwModel;
public GameObject powerSupplyModel;
public GameObject hardDriveModel;
public GameObject crosspeiceModel;
public GameObject fanModel;
public GameObject graphicsModel;
public GameObject ramModel;
public GUI toogle;
GUIStyle biggerFont;
public void Start()
{
StreamReader reader = null;
FileInfo theSourceFile = null;
theSourceFile = new FileInfo(Application.dataPath + "/puzzles.txt");
if (theSourceFile != null && theSourceFile.Exists)
reader = theSourceFile.OpenText();
if (reader == null)
{
Debug.Log("puzzles.txt not found or not readable");
}
else
{
// Read each line from the file
while ((txt = reader.ReadLine()) != null)
{
StartCoroutine(KillOnAnimationEnd(1f));
Debug.Log("-->" + txt);
completeText += txt;
}
}
if (gameObject.GetComponent<Collider>() == null)
gameObject.AddComponent(typeof(BoxCollider));
biggerFont.fontSize = 40;
}
public void OnMouseEnter()
{
isClicked = true;
completeText += txt;
}
public void OnMouseExit()
{
isClicked = false;
completeText += txt;
}
public void OnGUI()
{
if ((isClicked) && (cdrwModel))
{
GUI.contentColor = Color.white;
GUI.Label(new Rect(5, 5, 400, 400), "<color=cyan><size=30>This is the </size></color>" + "<color=cyan><size=30>" + this.cdrw + "</size></color>");
GUI.Label(new Rect(15, 35, 400, 400), "Press <TAB> for more information");
if (Input.GetKey(KeyCode.Tab))
{
GUI.contentColor = Color.white;
GUI.Box(new Rect(1000, 5, 400, 400), "What You Should Know");
GUI.Label(new Rect(1135, 5, 400, 400), "___________________");
GUI.Label(new Rect(1145, 23, 400, 400), "<color=red><size=20>The </size></color>" + "<color=red><size=20>" + this.cdrw + "</size></color>");
GUI.Label(new Rect(15, 35, 400, 400), "Press <TAB> for more information");
GUI.contentColor = Color.red;
GUI.Label(new Rect(1000, 50, 400, 400), completeText);
}
}
else if ((isClicked) && (powerSupplyModel))
{
GUI.contentColor = Color.white;
GUI.Label(new Rect(5, 5, 400, 400), "<color=cyan><size=30>This is the </size></color>" + "<color=cyan><size=30>" + this.powerSupply + "</size></color>");
GUI.Label(new Rect(15, 35, 400, 400), "Press <TAB> for more information");
if (Input.GetKey(KeyCode.Tab))
{
GUI.contentColor = Color.white;
GUI.Box(new Rect(1000, 5, 400, 400), "What You Should Know");
GUI.Label(new Rect(1135, 5, 400, 400), "___________________");
GUI.Label(new Rect(1145, 23, 400, 400), "<color=red><size=20>The </size></color>" + "<color=red><size=20>" + this.powerSupply + "</size></color>");
GUI.Label(new Rect(15, 35, 400, 400), "Press <TAB> for more information");
}
}
public类OnClick:monobhavior
{
public bool isClicked=false;
字符串cdrw=“CD-RW”;
字符串powerSupply=“电源”;
字符串hardDriveName=“硬盘驱动器”;
字符串crosspeiceName=“Crosspeice”;
字符串fanName=“Fan”;
字符串graphicsName=“图形卡”;
字符串ramName=“Ram”;
公共字符串txt;
公共字符串completeText=“”;
公共游戏对象cdrwModel;
公共游戏对象电源模型;
公共游戏对象模型;
公共游戏对象交叉模型;
公共游戏对象模型;
公共游戏对象图形模型;
公共游戏对象ramodel;
公共图形用户界面;
GUIStyle-biggerFont;
公开作废开始()
{
StreamReader=null;
FileInfo theSourceFile=null;
sourcefile=newfileinfo(Application.dataPath+“/puzzles.txt”);
if(theSourceFile!=null&&theSourceFile.Exists)
reader=theSourceFile.OpenText();
如果(读卡器==null)
{
Debug.Log(“puzzles.txt未找到或不可读”);
}
其他的
{
//从文件中读取每一行
而((txt=reader.ReadLine())!=null)
{
start例程(KillOnAnimationEnd(1f));
Debug.Log(“-->”+txt);
completeText+=txt;
}
}
if(gameObject.GetComponent()==null)
AddComponent(typeof(BoxCollider));
biggerFont.fontSize=40;
}
在MouseCenter()上公开作废
{
isClicked=true;
completeText+=txt;
}
在mouseExit()上的公共void
{
isClicked=false;
completeText+=txt;
}
公营机构
{
如果((单击)和&(cdrwModel))
{
GUI.contentColor=Color.white;
Label(新的Rect(5,5400400),“这是”+“”+This.cdrw+”;
标签(新的Rect(15,35,400,400),“按以获取更多信息”);
if(Input.GetKey(KeyCode.Tab))
{
GUI.contentColor=Color.white;
Box(新的Rect(1000,5400400),“您应该知道什么”);
标签(新的Rect(1135,5400400),“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
Label(新的Rect(114523400400),“The”+“”+this.cdrw+”;
标签(新的Rect(15,35,400,400),“按以获取更多信息”);
GUI.contentColor=Color.red;
标签(新的Rect(1000,50,400,400),completeText);
}
}
否则如果((单击)和(&(电源型号))
{
GUI.contentColor=Color.white;
Label(新的Rect(5,5400400),“这是”+“”+This.powerSupply+”;
标签(新的Rect(15,35,400,400),“按以获取更多信息”);
if(Input.GetKey(KeyCode.Tab))
{
GUI.contentColor=Color.white;
Box(新的Rect(1000,5400400),“您应该知道什么”);
标签(新的Rect(1135,5400400),“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
标签(新的Rect(114523400400),“The”+“”+this.powerSupply+”;
标签(新的Rect(15,35,400,400),“按以获取更多信息”);
}
}
呼叫
GUI.Label(new Rect(1000, 50, 400, 400), completeText);
不将completeText变量绑定到标签。它仅将completeText中的值复制到标签中
对completeText的进一步更改不会影响标签显示的内容。您的类定义在哪里?completeText定义了什么上下文?您不能在函数中定义函数。您提供的代码甚至不会编译。因此“不会显示任何内容”这几乎不是问题的描述…感谢您的回复,抱歉,我现在添加了完整的代码-仅包括两个模型的if语句请在此处发布代码时删除额外的连续空行。这确实会影响可读性(滚动太多,无法查看代码).ok-更好的代码-现在-哪一个“如果”有问题?这一行写在
public void OnGUI()
函数中,它会在每个UI框架上运行。因此,即使没有绑定,它也会更新。感谢您的回复,您建议我做些什么或研究以确保它出现?