txt loading works in play mode, but doesn't when it's exported as apk

Hello community,

I am using the following code to load in a txt file and update three 3d-text objects.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public class OfflineData : MonoBehaviour {
    public string textFileName;

    // Input Parameter
    [Header("Namen", order=1)]
    public GameObject[] Namen;

    [Header("Nachrichten", order=2)]
    public GameObject[] Nachrichten;

    [Header("Zeilenlänge & Geschwindigkeit", order=3)]
    public float line = 120;
    public float speed = 8;

    [Header("Icons", order = 4)]
    public GameObject[] iconList;
    public Sprite[] icons;
    private SpriteRenderer iconRenderer;
  

    // Private Members
    private List<DataObject> DataObjects;
    private int CurrentIndex = 0;

    // Use this for initialization
    void OnEnable () {

        // Dateiinhalt laden
        var sr = new StreamReader(Application.dataPath + textFileName);
        var fileContents = sr.ReadToEnd();
        sr.Close();

        // Dateiinhalt parsen
        this.DataObjects = fileContents
            .Split('\n')
            .Select(x => x.Split(';'))
            .Select(line => new DataObject { Person = line[0], Message = line[1], PictureNumber = line[2]})
            .ToList();

        StartCoroutine (ChangeNews ());
    }

    // Update is called once per frame
    IEnumerator ChangeNews () {

        /*
        // Texte setzen
        (this.Nachrichten[0].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 0).Message;
        (this.Nachrichten[1].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 1).Message;
        (this.Nachrichten[2].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 2).Message;
        */

    var firstMessage = this.Nachrichten.ElementAtOrDefault (0).GetComponent (typeof(LineBreaker)) as LineBreaker;
        firstMessage.UnwrappedText = this.DataObjects.ElementAt(this.CurrentIndex + 0).Message;
        firstMessage.NeedsLayout = true;
        firstMessage.MaxWidth = line;
        iconList[0].GetComponent<SpriteRenderer>().sprite = icons[int.Parse(this.DataObjects.ElementAt(this.CurrentIndex + 0).PictureNumber)];

        var secondMessage = this.Nachrichten.ElementAtOrDefault (1).GetComponent (typeof(LineBreaker)) as LineBreaker;
        secondMessage.UnwrappedText = this.DataObjects.ElementAt(this.CurrentIndex + 1).Message;
        secondMessage.NeedsLayout = true;
        secondMessage.MaxWidth = line;
        iconList[1].GetComponent<SpriteRenderer>().sprite = icons[int.Parse(this.DataObjects.ElementAt(this.CurrentIndex + 1).PictureNumber)];


        var thirdMessage = this.Nachrichten.ElementAtOrDefault (2).GetComponent (typeof(LineBreaker)) as LineBreaker;
        thirdMessage.UnwrappedText = this.DataObjects.ElementAt(this.CurrentIndex + 2).Message;
        thirdMessage.NeedsLayout = true;
        thirdMessage.MaxWidth = line;
        iconList[2].GetComponent<SpriteRenderer>().sprite = icons[int.Parse(this.DataObjects.ElementAt(this.CurrentIndex + 2).PictureNumber)];



        // Name setzen
        (this.Namen[0].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 0).Person;
        (this.Namen[1].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 1).Person;
        (this.Namen[2].GetComponent(typeof(TextMesh)) as TextMesh).text = this.DataObjects.ElementAt(this.CurrentIndex + 2).Person;


        // Warten
        yield return new WaitForSeconds (speed);

        // Aktuelle Zählervariable hochzählen
        if (this.CurrentIndex + 3 == this.DataObjects.Count())
            this.CurrentIndex = 0;
        else
            this.CurrentIndex++;

        // Loop wiederholen
        StartCoroutine(ChangeNews());       
    }

    class DataObject {
        public string Person;
        public string Message;
        public string PictureNumber;
    }
}

The code works well when I press in Unity the play button. But when I export my app as apk, the text from the txt file doesn’t get loaded.
Am I using expressions, that are not supported by Android? Or what is my fault?

Your text file is getting bundled inside a Unity package file, so it doesn’t exist as an individual file.

You can put stuff in the Resources directory and use Resources.Load instead. Or put the file in StreamingAssets then use Application.streamingAssetsPath. If you are loading from the internet, then save your data in Application.persistentDataPath

StreamingAssets and the persistent data path will have individual files on Android. I.e. you can use System.IO with those files.

Everything in the Resources directory gets bundled up into a Unity asset so you must use Resources.Load for those files.

Thank you, captain!

TextAsset txt = Resources.Load<TextAsset>(textFileName);
        MemoryStream content = new MemoryStream(txt.bytes);
        var sr = new StreamReader(content);

did the work :slight_smile: