- Home /
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!
Answer by phxvyper · Mar 24, 2017 at 05:54 AM
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:
Answer by UnityCoach · Mar 24, 2017 at 10:51 AM
If what you want is to store game data, you can use Xml serialisation, or Json serialisation, along with ScriptableObject.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113837im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help using LitJson 1 Answer
Which of the Two is Better?? 2 Answers
Faster alternative to GetComponentInParent() method? 1 Answer