Unity3D在Android平台上构建时,文本不显示

3
它在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. }

以下是屏幕截图。

Unity'

但这些文本在Android上并未显示。

Android'

我该如何解决这个问题?

文本文件位置:Assets/Resources/Magnelli_0.27.txt


文本应该出现在哪里?此外,您需要清理很多内容。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
你正在循环中执行GetComponent<Text>().text =,所以每次执行时,它都会覆盖你之前设置的文本。 - Foggzie
@GuntherFox 非常感谢!我已经编辑了我的代码 :) 覆盖文本就是我想要的。在Unity游戏视图上它运行良好,但在Android上不起作用(T_T) - Pat.AA
@GuntherFox 它可以在安卓上运行!!!!!!!!非常感谢!!!!!!!!哇!!!!!!!我真的非常非常感激!!!!!!!! - Pat.AA
没问题!很高兴它对你有用 :D - Foggzie
2个回答

2

我解决了这个问题!!我按照 @Gunther Fox 的评论操作,在安卓设备上成功了!

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

没成功(T.T)StringReader streader = new StringReader(asset.text); - Pat.AA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接