Search code examples
c#androidunity-game-enginestore

How to store txt file (reading/writing) and build for Android


I'm making a game about questions shared between people, and I want to add natively some questions to the game. In the way I made this game, I need to store a text file in Unity, that contains on each line information about these questions. I need to read and write on that txt file at runtime.

So I basically made a file questions.txt in the Resources folder, and it's actually fine in the Unity Editor: I can write and read as I want.

However, when I tried on my android device, the txt file couldn't be found.

I read that it's because the Resources folder isn't built on android. Then, I'm lost. I've read about TextAsset, but it's read-only. I Couldn't find a way to make this work with Application.dataPath and Application.persistentDataPath, because the file is actually not created with some script.

That's why I'm asking for some help now. I'm a bit lost and I just want some hints on how to implement that.

Thanks for taking some time to read and help. :)


Solution

  • I figured out how to, in my case at least. Made a script that contains a public TextAsset. I assigned my questions.txt to that TextAsset through the inspector.

    TextAsset is read-only. But here is the tip: I completly copied the content of that file to another that I created in Application.persistentDataPath, which allows reading and writing.

    Then, if I want to add native questions the game, all I need to do is copy the lines I need from the TextAsset I can modify before building.

    Thanks to @blackapps for the little hint. Solved!