Loading datas fom a textfile

Hello *,

I’d like to load a textfile and read the datas at runtime (manual, default settings etc).

To show exactly what I mean I have written a short code sample in Delphi:

st := TStringList.create; 
st.loadFromFile('level1.txt'); 
 
iLives := strToInt(st[0]); 
fTime := strToFloat(st[1]); 
 
st.free;

It is not possible to load the variables with PlayerPrefs because if I use PlayPrefs I have to save the variables before. In my example the variables are stored in a textfile which comes with the application.

Sorry for my English :frowning:

Thank you,
Ulrich

http://forum.unity3d.com/viewtopic.php?t=5531&highlight=read+files

Thank you, that ios wat I’m looking for. Sadly I cannot open my File.

If I write:

sr = new StreamReader("Setup.txt");

I will get the error message: Could not find file “E:\Unity\ReadWriteSample\Datas\Setup.txt”.

The file ist stored in: “E:\Unity\ReadWriteSample\Assets\own\Datas\Setup.txt” and I can’t make Unity to look in the correct folder.

:frowning:

Ulrich

If the text doesn’t change, use a TextAsset object.

Hello *,

now I have found a solution to load a textfile. Thank you for helping.

I’m usting:

sr = new StreamReader(Application.dataPath + "/Level/Level1.txt"); 
line = sr.ReadLine();
while (line != null) { 
   print(line);
   line = sr.ReadLine(); 
}

Is there also a chance to load e.g. the 3rd line like: line = sr.ReadLine(2); ?

Ulrich

Text Assets are serialized like all other assets in a build. There is no physical text file included when you publish your game.

ie. the text file is tagged as a dependency and will be included in the build.

If you don’t use TextAsset, at a later date you may wonder why it can’t find the text file, Unity didn’t actually include it.

Each call to ReadLine() is progressive, and not in relation to the start of the file.
So to read all the lines in the file.

while((line = sr.ReadLine()) != null)
{
print(line);
}

thank you, this is very important to know!

Ulrich

When I build to iPhone, it does not read the file just like you said. How do I revise this code so the iphone can read my txt file.

Here is my code (i have commented out different methods I have tried (which don’t work on the iphone) DictionaryFile (commented out) is a textasset. Still wont work.

void ParseDict()
    {
        //string path = @"Assets/Dictionary.txt";
		//string path = DictionaryFile.text;
		string path = Application.dataPath + "/Text/Dictionary.txt";
        try
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader(path))
            {
                string line;

                // Read and display lines from the file until the end of
                // the file is reached.

                while ((line = sr.ReadLine()) != null)
                {
                    dictionary.Add(line, line);
                }
            }
        }
        catch (System.Exception e)
        {
            // Let the user know what went wrong.
            UnityEngine.Debug.Log("The file could not be read:");
            UnityEngine.Debug.Log(e.Message);
        }

    }