[System.Serializable]
public class GameData
{
public List Namestring;
``}
How do I add an item to the list save it in a file and read it
Like if I have a static class with the name save
Go write
Public legname;
save.loadfile (); Reads in the file
legname = save.Namestring [0];
I can save single variables but I can’t save arrary or lists.
Can you help me with an example of a script that saves to file and then reads?
Thank you so much
Basically what you want to do is generally referred to as [Serialization][1] (for saving the runtime object data [to a file, or while transferring over the network, or otherwise]) and Deserialization (for loading the data back into runtime objects).
There are many methods to do so, and many formats, each with its own pros and cons (and some [dangers][2]). [Some][3] [benchmarks][4] and [comparisons][5].
Unity has added support for [JSON Serialization][6], and C# has built-in
[7] and [Binary][8] serialization, though I wouldn't recommend using the XmlSerializer since it's slow.
You can find many examples on the web for serialization/deserialization with unity, like [this][9] or [here][10].
As a quick practical example, you could implement the following utility script:
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public static class SerializationUtility
{
public static void Save(object data, string filePath)
{
using (StreamWriter writer = new StreamWriter(filePath))
{
// this will convert the object instance to a JSON document: the value and name of each field will be written to the file
// more about JSON here: https://www.json.org/json-en.html
string serializedObject = JsonUtility.ToJson(data);
// this will actually write the JSON string into a file on the disk
writer.Write(serializedObject);
}
}
public T Load<T>(string filePath)
{
using (StreamReader reader = new StreamReader(filePath))
{
// this will simply read the contents of the file from the disk into a string
string jsonContent = reader.ReadToEnd();
// this will convert the JSON file back into the object: each field will be populated with the saved value according to its name
T deserializedData = JsonUtility.FromJson<T>(jsonContent);
// return the newly created object, or you can simply feed the values back into this instance
return deserializedData;
}
}
}
Or read **[this thread][11]** for more in-depth insights and more robust solutions.
[1]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/
[2]: https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
[3]: https://mkhndrsn.wordpress.com/2013/11/26/comparison-of-methods-of-serialization-in-c/
[4]: https://stackoverflow.com/a/22782692/618726
[5]: https://en.wikipedia.org/wiki/Comparison_of_data-serialization_formats
[6]: https://docs.unity3d.com/Manual/JSONSerialization.html
[7]: https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer?view=net-6.0
[8]: https://docs.microsoft.com/en-us/dotnet/standard/serialization/binary-serialization
[9]: https://www.linkedin.com/pulse/binary-serialization-unity-samuel-arminana/
[10]: https://www.gamedeveloper.com/business/introduction-to-unity-serialization-and-game-data
[11]: https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity