2015-09-01 45 views
-1

我想知道爲什麼我的completeText變量不會攜帶到我用於GUI標籤的IF語句中,這些標籤在用戶懸停在不同模型上時顯示。我成功地使用StreamReader導入了文本,但是當我放入時:文本變量不會轉移到IF語句

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

       } 
      } 
+0

你的班級定義在哪裏? completeTxt定義了什麼上下文? – Steve

+0

您不能在函數內部定義函數。你提供的代碼甚至不會編譯*。所以「沒有出現」幾乎不是對問題的描述... – David

+0

感謝您的回覆,對不起,我現在添加了完整的代碼 - 包括只有兩個if語句的模型 – Jaindreas

回答

0

呼叫

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

的completeText變量到標籤不結合。它只是將completeText中的值複製到標籤中。

對completeText的進一步更改不會影響標籤顯示的內容。

+1

該行被寫入'public void OnGUI()'函數,它在每個單獨的UI框架上運行。所以它會更新,即使沒有綁定。 – maksymiuk

+0

謝謝您的回覆,您會建議我做什麼或進行調查以確保其顯示? – Jaindreas