Pat.AA
Pat.AA

Reputation: 51

Unity3D text doesn't show up when it build in Android

It works well on Unity, but it couldn't when it build in Android. Here is my code.

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. }

and here are screenshots.

Unity'

but these text doesn't show up on Android.

Android'

How can I solve this problem?

The Text File Location: Assets/Resources/Magnelli_0.27.txt

Upvotes: 3

Views: 880

Answers (2)

Pat.AA
Pat.AA

Reputation: 51

I solved the problem!! I followed @Gunther Fox 's comment, and it was working on 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. }

Upvotes: 2

Cổ Ch&#237; T&#226;m
Cổ Ch&#237; T&#226;m

Reputation: 328

Please put file Magnelli_0.27 as TextAsset into Resouce folder and reader line per line with StringReader. I think when u build for another OS path valuable will wrong and then it not show up text.

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

Upvotes: 0

Related Questions