Search Unity

text file in unity, work ok, but not in apk

Discussion in 'Android' started by tiagolobodias, Jul 5, 2014.

  1. tiagolobodias

    tiagolobodias

    Joined:
    Jun 28, 2014
    Posts:
    1
    hi,
    I want to use a text file, so i am using the script below.
    It works in pc. I am able to read the file "ficheiro.txt" but when i create the APK it does not work.

    I created the folder: assets/Resources and went to Assets+import new asset and added the file
    I run the game on PC and it's ok.
    When testing the APK in Android, the "game" works fine but when i press a button to read the file, it does not read it.
    If i open the apk with 7-zip it does not show the file inside, i guess it should be there. Or the file is something like in machine code and i cannot see it?

    Am i importing the file to unity in a wrong way? How should i do it?
    Please can you help me? I don't know if Streamreader is the right way to do it, but it works fine in pc.
    I guess when i am building the apk, unity does not include the file in apk...

    Thank you for your time.


    try
    {
    // Create an instance of StreamReader to read from a file.
    // The using statement also closes the StreamReader.
    //using (StreamReader sr = new StreamReader("ficheiro.txt"))
    using (StreamReader sr = new StreamReader(Application.dataPath + "/Resources/ficheiro.txt"))
    {
    string line;
    // Read and display lines from the file until the end of
    // the file is reached.

    while ((line = sr.ReadLine()) != null)
    {
    Console.WriteLine(line);
    print("ficheiro a ler ok"+line);
    ws_script1_texto_fich=line;

    }
    }
    }
    catch (Exception e)
    {
    // Let the user know what went wrong.
    Console.WriteLine("The file could not be read:");
    print("erro ficheiro");
    Console.WriteLine(e.Message);
    }
     
  2. christides11

    christides11

    Joined:
    May 19, 2012
    Posts:
    667
    First, surround your code with code tags like this
    Code (csharp):
    1.  
    2. code
    3.  
    On android, are you creating the folder at all in your script before you write to it? Because if it doesn't exist, it can't write to it.
     
  3. langem

    langem

    Joined:
    Mar 18, 2013
    Posts:
    73
unityunity