Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to include .txt file on iphone!

Discussion in 'iOS and tvOS' started by spentak, Jul 14, 2010.

  1. spentak

    spentak

    Joined:
    Feb 11, 2010
    Posts:
    246
    This works beautifully when testing it on the computer. But the iphone isn't including or recognizing my .txt file on the device. If any way possible, I don't want to change my stream reader. Hoping I can just include a path. Here is my code:

    Code (csharp):
    1.  
    2. void ParseDict()
    3.     {
    4.         string path = @"Assets/Dictionary.txt";
    5.        
    6.        
    7.         try
    8.         {
    9.             // Create an instance of StreamReader to read from a file.
    10.             // The using statement also closes the StreamReader.
    11.             using (StreamReader sr = new StreamReader(path.ToString()))
    12.             {
    13.                 string line;
    14.  
    15.                 // Read and display lines from the file until the end of
    16.                 // the file is reached.
    17.  
    18.                 while ((line = sr.ReadLine()) != null)
    19.                 {
    20.                     dictionary.Add(line, line);
    21.                 }
    22.             }
    23.         }
    24.         catch (System.Exception e)
    25.         {
    26.             // Let the user know what went wrong.
    27.             UnityEngine.Debug.Log("The file could not be read:");
    28.             UnityEngine.Debug.Log(e.Message);
    29.         }
    30.  
    31.      
    32.  
    33.     }
    34.  
     
  2. spentak

    spentak

    Joined:
    Feb 11, 2010
    Posts:
    246
    I saw a post that said this:


    "<path to player app bundle>/<AppName.app>/Data

    You just need to concatenate a slash and the name of the file to the end of the string returned by Application.dataPath.
     
  3. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,107
  4. spentak

    spentak

    Joined:
    Feb 11, 2010
    Posts:
    246
    I tried using
    string path = TextAsset.text , but that didn't work.


    This doesn't work either:

    Code (csharp):
    1.  
    2. string path = Application.dataPath.Substring (0, Application.dataPath.Length - 5);
    3.         // Strip application name
    4.         path = path.Substring(0, path.LastIndexOf('/'));  
    5.         path += "/Documents/Dictionary.txt";
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    if you drop a txt into the project it will be treated as text asset and integrated in the generated bundle. it will no longer exist as file.

    as smag mentioned, use the TextAsset class to get the content of the txt for further processing.
     
  6. spentak

    spentak

    Joined:
    Feb 11, 2010
    Posts:
    246
    Yes, but how do I read lines from a TextAsset with my Streamreader??
     
  7. mudloop

    mudloop

    Joined:
    May 3, 2009
    Posts:
    1,107
    You don't need a streamreader, you read it by doing textAsset.text
     
  8. spentak

    spentak

    Joined:
    Feb 11, 2010
    Posts:
    246

    But i'm trying to read each line in the text file into a c# Dictionary - 1 line per dictionary value. I'm not sure how to do that without streamreader
     
  9. Fenrisul

    Fenrisul

    Joined:
    Jan 2, 2010
    Posts:
    617
    String.Split is handy for this stuff.

    its c# syntax should be pretty normal
    its javascript syntax is a bit funky though

    JS:
    var strArr : String[] = someString.Split("\r"[0]);

    will return 1 array entry per line (assuming you use a CR line-ending)

    look up String.Split() in the monodocs for more info.

    Also warning: I ran into some issues with text using CRLF line endings when porting from PC Unity2.6 to iPhone1.x
     
  10. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    either that or read out the full text and then write it out to the projects cache folder and read it from there like a regular file again.
     
  11. spentak

    spentak

    Joined:
    Feb 11, 2010
    Posts:
    246
    Ok, so after many hours of frustration and trial and error I fixed the problem.

    I used this to feed into the stream reader:
    string path = Application.dataPath + "/Dictionary.txt";

    I then built the game in unity. I then physically copied the file Dictionary.txt into the "Data" folder in the Unity game build folder. I then Built and Ran from xcode. Works now.

    The revised code:
    Code (csharp):
    1. void ParseDict()
    2.     {
    3.        
    4.         string path = Application.dataPath + "/Dictionary.txt";
    5.        
    6.         try
    7.         {
    8.             // Create an instance of StreamReader to read from a file.
    9.             // The using statement also closes the StreamReader.
    10.             using (StreamReader sr = new StreamReader(path))
    11.             {
    12.                 string line;
    13.  
    14.                 // Read and display lines from the file until the end of
    15.                 // the file is reached.
    16.  
    17.                 while ((line = sr.ReadLine()) != null)
    18.                 {
    19.                     dictionary.Add(line, line);
    20.                 }
    21.             }
    22.         }
    23.         catch (System.Exception e)
    24.         {
    25.             // Let the user know what went wrong.
    26.             UnityEngine.Debug.Log("The file could not be read:");
    27.             UnityEngine.Debug.Log(e.Message);
    28.         }
    29.  
    30.  
    31.     }
     
  12. SamTHorn

    SamTHorn

    Joined:
    Jun 21, 2010
    Posts:
    3
    Cool this would be awesome if I could only get it working..

    Cheers and thanks for the share.
     
  13. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Any solution to get this done automatically?
     
  14. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,600
  15. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Ah, sometimes the needed information is hard to find in the docs....
    So, to get a specific file I would write

    path = Application.dataPath + "/Raw/MyPointCache.pc2";

    Right?

    Thanks!
     
  16. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,600
  17. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347