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:
void ParseDict()
{
string path = @"Assets/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.ToString()))
{
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);
}
}
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:
void ParseDict()
{
string path = Application.dataPath + "/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);
}
}