Reading from Text File

Hi, sorry if this has been asked a thousand times before, but I can’t find a clear definitive answer to my exact questions here or in the documentation. I basically want to load data from a text file when the game starts up, but it has to have the following features:

  1. Has to be in the same place between when I’m building in the Unity editor, and after I compile the game and ship it. I suppose this isn’t too important since even though I’m going to have many such files, I could have the file path stored once and read from that, it’ll just be a pain if I’m testing at the end and have to keep switching it back and forth.

  2. The default file I make has to get shipped with the game, but remain separate (as in, you could still open it up in, say, Notepad, and edit it, and the game will load the edited file when it starts up. TextAsset, as I understand it, bundles the file up in the executable, so that won’t work).

I’ve read about Application.persistentDataPath, but the location is in some random area under C:\users.… not in the project I’m making (which is just under C:<GAME NAME>). This makes me nervous that the file won’t be there when the game is installed.

Thanks for your help in advance!

Edit: The answer needs to be usable in C#, I don’t know Javascript.

Read Text File (how to read text files)
Application.persistentDataPath (appdata folder type location)
Application.dataPath (game folder)

1 Like

Application.persistentDataPath is best used for saving user data - save games, preferences, things like that. If your user updates the game and you want the file to still be there, use persistentDataPath. And you should expect this folder to be empty or even nonexistent when you first use it.

Application.dataPath is the path to your app executable itself. This sounds useful, but what I think would be better is the next option.

Application.streamingAssetsPath might be more useful to you. Anything in your StreamingAssets folder will be bundled up alongside your app, and accessible at this path.

It sounds like you may want a combination of these.

  1. Look for the file at .persistentDataPath
  2. If it doesn’t exist, copy your “default” file from .streamingAssets to .persistentDataPath
  3. Load the file from .persistentDataPath

This will work consistently in your editor and standalones on the same machine, and will automatically create the file for your users when running on a new machine. .persistentDataPath, I believe, is the same between standalone and editor on the same machine, so if you’ve made changes, it’ll find those changes on both.

3 Likes

Star Manta, I’ve tried your method, and it works (thanks!), however now if I make any changes to the text file (which I am likely to do frequently as development goes on), I have to go delete the old copy in persistentDataPath, which is a pain. Any thoughts?

Try using a version number. Something like this…

Add a number to the top of your file and, after checking the file exists, also check that the value is equal to your default’s. If it is less than the value in the default version, replace the file in the persistent path. When you make changes to the default, increment the revision number.

2 Likes

If you do the version number, make sure you save a copy of the user’s file before you replace it. Nothing more annoying than an app overwriting changes I made with no chance of getting it back.

@LaneFox Thank you for the link! That first one did it for me. :):):):slight_smile: