Importing from an external .txt file

Hey everyone, it’s been a while since I’ve done some programming so I’m a bit out of practice. For my game, I’m wanting to store each level as a 2D array in an external .txt file. I’m not too familiar with the C# syntax for doing file I/O nor am I too familiar with how Unity handles file I/O. I’m just looking for some pointers or at the very least a nudge in the right direction. My game’s target platform is Android, so I don’t know if there are any differences in regards to how external files are handled.

Thanks all!

You can use File.ReadAllText and File.WriteAllText to write a string to a file. There is also File.ReadAllLines and File.WriteAllLines for arrays.

If you want to get into more stable and professional file IO, you’d use StreamReader and StreamWriter which are stream classes.

On android you’ll have to ensure that you give your app the proper permissions in manifest so that it has access to the SD Card and you should be able to save data from there AFAIK


Here are some links:

File.ReadAllText

File.WriteAllText

File.ReadAllLines

File.WriteAllLines


StreamReader

StreamWriter

If what you want is to store game data, you can use Xml serialisation, or Json serialisation, along with ScriptableObject.