2016-08-05 61 views
-2

In this picture,日誌打印漂亮! 但統一輸出是錯誤的。 我該如何解決它?Unity + php +數據庫數據輸出問題

這是我的源:

private void Awake() 
    { 
     info = new Dictionary<string, string>(); 

    } 


    private IEnumerator Start() 
    { 

     WWW dash = new WWW("http://localhost/test.php"); 
     yield return dash; 

     string[] Separators = new string[] { "\n" }; 
     string[] lines = dash.text.Split(Separators, System.StringSplitOptions.RemoveEmptyEntries); 

     info.Clear(); 
     for(int i=0; i<lines.Length; i++) 
     { 
      GameObject tmp = Instantiate(scorepre); 
      string[] parts = lines[i].Split(','); 
      string Name = parts[1]; 
      string Detail = parts[2]; 
      info.Add(Name, Detail); 
      Debug.Log(Name + " - " + Detail); 

      named.GetComponent<Text>().text = Name; 
      detailed.GetComponent<Text>().text = Detail; 

      tmp.transform.SetParent(infoParent); 
     } 
+0

are are named and detailed game objects defined? – ColmanJ

+0

是的,我定義了。 –

+0

private Dictionary info; public Transform infoParent; public GameObject scorepre; public GameObject named; public GameObject詳細說明; 這些都是我的定義。 –

回答

1

命名和詳細的對象似乎總是指向同一個對象。

編輯 重新思考你可能如何鏈接你的對象後,我認爲代碼的順序是不正確的。

private IEnumerator Start() 
{ 
    WWW dash = new WWW("http://localhost/test.php"); 
    yield return dash; 

    string[] Separators = new string[] { "\n" }; 
    string[] lines = dash.text.Split(Separators, System.StringSplitOptions.RemoveEmptyEntries); 

    info.Clear(); 
    for(int i=0; i<lines.Length; i++) 
    {   
     if (i > 0) // only copy the object after the first text is set 
     { 
      GameObject tmp = Instantiate(scorepre); 
      tmp.transform.SetParent(infoParent); 
     } 

     string[] parts = lines[i].Split(','); 
     string Name = parts[1]; 
     string Detail = parts[2]; 
     info.Add(Name, Detail); 
     Debug.Log(Name + " - " + Detail); 

     //first change the text 
     named.GetComponent<Text>().text = Name; 
     detailed.GetComponent<Text>().text = Detail; 
    } 
} 
+0

對不起。我太晚了。謝謝您的回答!!。但它不起作用... –

+0

我有一個錯誤。源代碼對我來說非常好!非常感謝 –

+0

@lukedavis這個解決方案有效嗎? – ColmanJ