0

So, I'm developing Visual Novel in unity. The application is running smoothly in the pc but now I want it to run in android but the contents in my .txt file is not showing in screen. I assume that the error is that the text file in StreamingAssets folder couldn't be loaded and I don't know why. Here's my code:

 void Start () {
    path = "jar:file://" + Application.dataPath + "!/assets/StreamingAssets/Dialogue0.txt";

    lines = new List<DialogueLine>();
    Example();
}

IEnumerator Example()
{
    if (path.Contains("://"))
    {
        var www = new WWW(path);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError("Can't read");
        }
        LoadDialogue(www.text);
    }
    else
        LoadDialogue(path);
}



 void LoadDialogue(string filename)
{
    string line;
    StreamReader r = new StreamReader(filename);

    using (r)
    {
        do
        {
            line = r.ReadLine();
            if (line != null)
            {
                string[] lineData = line.Split('|');
                if (lineData[0] == "Player")
                {
                    DialogueLine lineEntry = new DialogueLine(lineData[0], "", 0, 0, "");
                    lineEntry.options = new string[lineData.Length - 1];
                    for (int i = 1; i < lineData.Length; i++)
                    {
                        lineEntry.options[i - 1] = lineData[i];
                    }
                    lines.Add(lineEntry);
                }
                else
                {
                    DialogueLine lineEntry = new DialogueLine(lineData[0], lineData[1], int.Parse(lineData[2]), int.Parse(lineData[3]), lineData[4]);
                    lines.Add(lineEntry);
                }
            }
        }
        while (line != null);
        r.Close();
    }
}

Path of my .txt file

Contents of my .txt file

Eve
  • 43
  • 1
  • 10
  • I thing your path should only be `path = "jar:file://" + Application.dataPath + "!/assets/Dialogue0.txt";` or use `path = Application.streamingAssetsPath + "/Dialogue0.txt` instead – Senbazuru Aug 09 '18 at 08:32
  • does "Application.streamingAssetsPath" is working? because according to the documentation in unity3d: https://docs.unity3d.com/Manual/StreamingAssets.html that was for pc, and for android it was this: path = "jar:file://" + Application.dataPath + "!/assets/"; – Eve Aug 09 '18 at 08:38
  • The Documentation states that it will always point to the correct path of each platform _It’s always best to use Application.streamingAssetsPath to get the location of the StreamingAssets folder, as it will always point to the correct location on the platform where the application is running._ Except UWP i think :) – Senbazuru Aug 09 '18 at 08:50
  • It's still not working. I know this sound stupid but Did I do right in my www code? or something missing?:( – Eve Aug 09 '18 at 09:00
  • Don't call a coroutine function directly. You have to call it with `StartCoroutine`. That should be `StartCoroutine(Example())`. See the `ReadFromStreamingAssets` function from the answer in the duplicate on how to read file from StreamingAssets. – Programmer Aug 09 '18 at 15:39

1 Answers1

0

Just serialize your file to Appliction.persistentDataPath + filename and then read easly from it .It will save file in Windows(AppData/LocalLow/InYourUnityProject) in android (under company name folder)

auslander
  • 470
  • 5
  • 17