Search Unity

Read text file that is included in the project

Discussion in 'Android' started by nielsvaes, Jul 8, 2013.

  1. nielsvaes

    nielsvaes

    Joined:
    Jul 29, 2012
    Posts:
    18
    I've been having some problems reading from a text file that is included in the project, because I can't figure out how to do it :)

    I have text file in the root of my project. When playing in the editor in Windows everything works fine. When trying to play a build on an Android device, the file can't be found.

    Code (csharp):
    1.  
    2.     void Start ()
    3.     {
    4.         string fileLocation = Application.persistentDataPath + "file.txt";
    5.  
    6.         if (File.Exists(fileLocation))
    7.         {
    8.             // STUFF HAPPENS HERE
    9.         }
    10.         else
    11.         {
    12.             Debug.LogError("Can't find file");
    13.         }
    14.     }
    15.  
    16.  
    According to logcat, the Android app is looking for the file here:

    but when I use ES Explorer there isn't a file there.


    So I'm wondering what I need to do in order to make the Android build find the text file.
     
  2. Raigex

    Raigex

    Joined:
    Jul 6, 2012
    Posts:
    154
    You should probably put the file in the streamed assets folder. I believe this makes it so that it is stored inside the apk itself.
    Or you can store it in the resources folder, which allows you Load the resource at runtime and since it is a text file you can then read the text file itself.
     
  3. nielsvaes

    nielsvaes

    Joined:
    Jul 29, 2012
    Posts:
    18
    I'm not really having any luck with Resources.Load or streamed assets.

    Do you know what exactly you need to load the text file as? The documentation says things like

    Code (csharp):
    1. go.renderer.material.mainTexture = Resources.Load("glass") as Texture;
    How would you do this with a text file? Resources.Load returns an object by default, to what kind of thing do I need to cast it?
     
  4. nikolic

    nikolic

    Joined:
    Oct 15, 2011
    Posts:
    162
    Reading text file "readme.txt" from the \Resources folder:

    Code (csharp):
    1. TextAsset txt = (TextAsset)Resources.Load("readme", typeof(TextAsset));
    2. string content = txt.text;
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use a TextAsset public variable and link it in the inspector.

    Code (csharp):
    1. public TextAsset textFile;
    --Eric
     
    twobob, Sami_AlEsh, Aseemy and 2 others like this.
  6. nielsvaes

    nielsvaes

    Joined:
    Jul 29, 2012
    Posts:
    18

    Thanks for clearing that up for me. I've got it working now. For anyone else looking at this in the future, keep in mind that you shouldn't specify the extension of the file in the resources folder. "readme.txt" doesn't work, but just "readme" (obviously) does.

    Thanks nikolic!
     
    naviln likes this.
  7. CyproNET

    CyproNET

    Joined:
    Sep 15, 2013
    Posts:
    79
    Good advice thanks!
     
  8. Danielthedev

    Danielthedev

    Joined:
    Aug 22, 2014
    Posts:
    1
    I have this problem too but Unity gives me every time errors so can someone give me a sample project?
     
  9. naviln

    naviln

    Joined:
    Oct 18, 2016
    Posts:
    32
    Perfect! thanks for the code sample, exactly what I needed.
     
  10. naviln

    naviln

    Joined:
    Oct 18, 2016
    Posts:
    32
    Very helpful, thanks
     
  11. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    OMG thanks you so much. The .txt at the end was causing the issue.
    Fixed all my problems. Thanks so much.
     
  12. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    2,058
    Wonder what is the oldest comment you ever posted that I liked. Gotta be pushing a decade Eric. My shocking memory (and need to wear 17 other hats) doffs a dusty cap

    EDIT: You would have loved the streamerReader implementation I just ripped, reading hard coded names WITH hard coded paths into a list of strings (that didn't even point to the right place when built) - that is wiped 10000 times and one word is chosen from it on each cycle...

    Are there words? yes, "improved on that"...