它在Unity上运行良好,但在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. }
以下是屏幕截图。
但这些文本在Android上并未显示。
我该如何解决这个问题?
文本文件位置:Assets/Resources/Magnelli_0.27.txt
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[]
的方式意味着您只会使用前两个值。 - FoggzieGetComponent<Text>().text =
,所以每次执行时,它都会覆盖你之前设置的文本。 - Foggzie