2017-03-16 119 views
3

它在Unity上運行良好,但在Android中生成時無法運行。這是我的代碼。Unity3D文本在Android版中不顯示

1. private string path;// = Application.dataPath + "/Resources/"; 
2. private string filename = "Magnelli_0.27.txt"; 
3. private TextAsset asset; 
4. private string str; 
5. private string[] names; 
6. private Vector3 ViewPosition; 
7. private double ViewZPosition; 
8. 
9. private int length; 
10. private double[] arrZ; 
11. private double[] arrCV; 
12. 
13. // Use this for initialization 
14. void Start() { 
15.  Screen.sleepTimeout = SleepTimeout.NeverSleep; 
16.  //AssetDatabase.ImportAsset(path); 
17.  //LoadTextFile(); 
18. 
19.  path = Application.dataPath + "/Resources/"; 
20.  asset = Resources.Load ("Magnelli_0.27")as TextAsset; 
21.  str = asset.ToString(); 
22.  names = str.Split('\n'); 
23.  length = names.Length; 
24. 
25.  Debug.Log ("length: " + length); 
26.  //Debug.Log ("str: " + str); 
27.  Debug.Log ("names[0]: " + names[0] + "names[1]: " + names[1]); 
28.  //Debug.Log (asset.text); 
29. } 
30. 
31. // Update is called once per frame 
32. void Update() { 
33. 
34.  ViewPosition = GameObject.FindWithTag("MainCamera").transform.position; 
35.  ViewZPosition = ViewPosition.z; 
36. 
37.  StreamReader reader = new StreamReader (path + filename); 
38. 
39.  TextReader txtreader = new StringReader (asset.text); 
40. 
41.  StringReader streader = new StringReader (asset.text); 
42. 
43.  string txt = ""; 
44. 
45.  arrZ = new double[length]; 
46.  arrCV = new double[length]; 
47. 
48.  for (int i = 0; i < length; ++i) { 
49. 
50.   txt = streader.ReadLine(); 
51.   //Debug.Log ("txt: " + txt); 
52.   string[] sprite = txt.Split (' '); 
53. 
54.   foreach (string b in sprite) { 
55.    //Debug.Log ("b: " + b); 
56.   } 
57. 
58.   
59.   
60.   arrZ[i] = Convert.ToDouble(sprite[0]); 
61.   arrCV[i] = Convert.ToDouble(sprite[1]); 
62.  } 
63.   
64.  // clear memories 
65.  reader.Dispose(); 
66. 
67.  for (int i = 0; i < length; i++) { 
68.   
69.   if ((arrZ[i])*100-0.2 < ViewZPosition & (arrZ[i])*100+0.2 > ViewZPosition) 
70.   { 
71. 
72.    GetComponent<Text>().text = "redshift z : " + ViewZPosition*0.01 + "\nComoving Volume : " + arrCV[i] + " Gpc³"; 
73.   } 
74.  } 
75. 
76.  reader.Close(); 
77.  txtreader.Close(); 
78. 
79. } 

這裏是屏幕截圖。

Unity'

但這些文字並沒有在Android上露面。

Android'

我怎樣才能解決這個問題?

文本文件地點:資產/資源/ Magnelli_0.27.txt

+0

哪裏是文本應該顯示出來?另外,你在這裏需要清理很多東西。 1)除了處理它並關閉它之外,你不會對'reader'做任何事情。 2)當你分配'str'時,你將它設置爲'asset.ToString()'而不是'asset.text'。 3)'arrZ = new double [length];''和'arrCV = new double [length];'可以在'Start()'的末尾發生,而不是'Update()'中的每一幀。 4)由於您已經使用了'Resouces.Load()',因此您不需要使用'path'和'StreamReader'。 5)你在第60和61行編制'sprite []'的方式意味着你將只使用前兩個值。 – Foggzie

+0

6)您在循環中執行GetComponent ().text =',所以每次發生時它都會覆蓋您之前設置的文本。 – Foggzie

+0

@GuntherFox非常感謝!我編輯我的代碼:)覆蓋文本是我想要的。它適用於Unity遊戲視圖,但它不適用於Android(T_T) –

回答

2

我解決了這個問題!我遵循@Gunther Fox的評論,它正在開發Android!

answer

1. private TextAsset asset; 
2. private string str; 
3. private string[] names; 
4. private Vector3 ViewPosition; 
5. private double ViewZPosition; 
6. private int length; 
7. private double[] arrZ; 
8. private double[] arrCV; 
9. 
10. // Use this for initialization 
11. void Start() { 
12.  Screen.sleepTimeout = SleepTimeout.NeverSleep; 
13. 
14.  asset = Resources.Load ("Magnelli_0.27")as TextAsset; 
15. 
16.  str = asset.text; 
17.  names = str.Split('\n'); 
18.  length = names.Length; 
19. 
20.  arrZ = new double[length]; 
21.  arrCV = new double[length]; 
22. 
23. } 
24. 
25. // Update is called once per frame 
26. void Update() { 
27. 
28.  ViewPosition = GameObject.FindWithTag("MainCamera").transform.position; 
29.  ViewZPosition = ViewPosition.z; 
30. 
31.  TextReader txtreader = new StringReader (asset.text); 
32.  StringReader streader = new StringReader (asset.text); 
33. 
34.  string txt = ""; 
35. 
36.  for (int i = 0; i < length; ++i) { 
37. 
38.   txt = streader.ReadLine(); 
39.   string[] sprite = txt.Split (' '); 
40. 
41.   arrZ[i] = Convert.ToDouble(sprite[0]); 
42.   arrCV[i] = Convert.ToDouble(sprite[1]); 
43.  } 
44.  for (int i = 0; i < length; i++) { 
45.   
46.   if ((arrZ[i])*100-0.2 < ViewZPosition & (arrZ[i])*100+0.2 > ViewZPosition) 
47.   { 
48. 
49.    GetComponent<Text>().text = "redshift z : " + ViewZPosition*0.01 + "\nComoving Volume : " + arrCV[i] + " Gpc³"; 
50.   } 
51.  } 
52. } 
0

請把文件Magnelli_0.27爲TextAsset到種源文件夾和讀卡器行,每行有StringReader。我想當你爲另一個操作系統路徑構建有價值的時候會出錯,然後它不會顯示文本。

https://msdn.microsoft.com/en-us/library/system.io.stringreader.readline(v=vs.110).aspx

+0

它沒有工作(噸) StringReader streader = new StringReader(asset.text); –

+0

哎喲,我認爲你需要檢查文件資產是資源文件夾呢.... –

相關問題