- Home /
Reading files on Android,
It's 1 AM and I have been trying to get this to work for some hours now. I just want to load a simple text file containing some numbers(created by a level generator), then I would use them to create a map. I am using this path: Path.Combine(Application.streamingAssetsPath, "level01.txt") Everything works in the editor but on the android device, only the UI is loaded. I am using unity version 2018/3/0f2. Sample file. Full code:
 using Assets.Scripts;
 using System.IO;
 using UnityEngine;
 
 public class MapGenerator : MonoBehaviour
 {
     public int width = 15;
     public int height = 15;
 
     public GameObject[] tileMap = new GameObject[6];
 
     private GameObject hexPrefab;
 
     public static  int[] tileType = new int[225];
     public static int[] tileAttr = new int[225];
 
     private const float scaleMultiplyer = 5f;
     private const float xOffset = 1.037f * scaleMultiplyer;
     private const float zOffset = 0.896f * scaleMultiplyer;
     private const float levitation = 0.5f;
     private const int size = 225;
 
     private void Start()
     {
         //Reading the numbers into an array
         string[] asd = new string[size];
         using (StreamReader sr = new StreamReader(Path.Combine(Application.streamingAssetsPath, "level01.txt"))) 
         {
             string line = "";
             for(int I = 0; I < size; I++)
             {
                 line = sr.ReadLine();
                 asd[I] = line;
             }  
         }
 
         //This is for getting the tile from the number
         for (int l = 0; l < tileType.Length; l++)
         {
             char[] row = asd[l].ToCharArray();
             tileType[l] = int.Parse(row[0].ToString());
             string newString = asd[l].Remove(0, 1);
             tileAttr[l] = int.Parse(newString);
         }
 
 
         //I am using a hexagonal map
         int i = 0;
         for (int x = 0; x < width; x++)
         {
             for (int y = 0; y < height; y++)
             {
                 float xPos = x * xOffset;
 
                 if (y % 2 == 1)
                 {
                     xPos += xOffset / 2f;
                 }
                 hexPrefab = tileMap[tileType[i]];
 
                 hexPrefab.GetComponent<attr>().arrtibutes = tileAttr[i];
 
                 GameObject hex = Instantiate(hexPrefab, new Vector3(xPos, 0.5f, y * zOffset), Quaternion.identity);
                 hex.name = "Hex_" + x + "_" + y;
                 hex.transform.SetParent(this.transform);
 
                 i++;
             }
         }
     }
 }
Answer by leminh_2014 · Aug 25, 2019 at 01:09 AM
have u tried to put this file in "Resource" folder ? If your project does not have you should create them via path:Assets/Resource.
Your answer
 
 
             Follow this Question
Related Questions
Android: Trying to reset save data on device when pushing a new build! 1 Answer
Android version not working as intended 2 Answers
Android version has very low FPS, but the iOS version is fine? 0 Answers
My game is working not in android build as it works in the editor 0 Answers
Unity 2017.3.0f3 / Android SDK Tools incompatibility 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                