There are several approaches for persisting data in Unity. Here is a my take:
Unity comes with a simple system to store key-value pairs cross platform. It only works for simple types like strings, bools and numbers, but it is the easiest to use as a beginner. Be aware that the system is intended to store small data bits like the players preferred volume, etc. It is not meant to persist level data or score. However, it’s fine for beginners to experiment with it to get things going. You can always switch later.
It is possible to convert classes marked with the System.Serializable attribute to a binary file by using the BinaryFormatter from the .NET framework (by Microsoft). This system is also very easy to use and can handle large amounts of data and is also very fast. The drawback is, that you might have (small) problems when going cross platform. For example, on iOS, you have to define certain compilation symbols before the serializer can work, because it relies on reflection. All of these issues can be solved with usually on or two lines of code. On Windows it will work fine out of the box. Another problem in the long run might be that you have to keep versioning your serialized structure everytime you change the data layout of your class. Meaning, if add or remove variables to/from your class, the program will be incompatible with the already saved files on disk. To fix this, you have to write special code to handle each version.
Simple/Custom Text Serialization
As shown in your example, you can simply write lines of text to a file. Maybe you want to save score values, then you could write each value in the format “LABEL, VALUE” on a single line. Then parse the file and reconstruct the information. This is usually called a CSV file (comma separated values), or generally a text file with key-value pairs of data. Similar to this (note, this might not compile and is not tested, but you can look up file operations easily):
int scoreA = 5;
int scoreB = 2;
string serializedData =
"ScoreA, " + scoreA.ToString() + "
"ScoreB, " + scoreB.ToString() + "
// Write to disk
StreamWriter writer = new StreamWriter("MyPath.txt", true);
StreamReader reader = new StreamReader("MyPath.txt");
string lineA = reader.ReadLine();
string splitA = lineA.Split(',');
scoreA = int.Parse(splitA);
string lineB = reader.ReadLine();
string splitB = lineB.Split(',');
scoreB = int.Parse(splitB);
There are also CSV-Reader/Writer libraries to handle the text parsing for you.
JSON/XML etc. Text Serialization
A robust way to save data is using existing (markup) languages/tools like JSON or XML. Some of them are builtin to Unity like the JsonUtility, others like XMLWriter are part of the .NET framework and there a multiple other 3rd party libraries you can use. This type of serialization is usually relatively slow, but can handle errors and versioning well. It’s also easy to debug save files and easy to use because of the existing documentation. There are many other standardized text file formats (e.g. YAML).
I hope this inspires some web searches.