文本变量未转入IF语句

时间:2015-09-01 18:39:30

标签: c# unity3d

我想知道为什么我的completeText变量不会进入我用于GUI标签的IF语句,当用户将鼠标悬停在不同的模型上时。我已成功使用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");

                }
            }

1 个答案:

答案 0 :(得分:0)

电话

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

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

对completeText的进一步更改不会影响标签显示的内容。