文本变量没有结转到IF语句

本文关键字:IF 语句 变量 文本 | 更新日期: 2023-09-27 18:10:41

我想知道为什么我的completeText变量不会携带到IF语句中,我使用的GUI标签显示为用户悬停在不同的模型上。我已经成功地导入文本使用StreamReader,但当我放入:

            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");
                }
            }

文本变量没有结转到IF语句

呼叫

GUI.Label(new Rect(1000, 50, 400, 400), completeText);

没有将completeText变量绑定到Label。它只是将completeText中的值复制到标签中。

对completeText的进一步修改不会影响Label显示的内容