2017-03-16 203 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

哎哟,我认为你需要检查文件资产是资源文件夹呢.... –