Create powerful and user-friendly editors for you and your team!
But you can also use Odin's serialization API to serialize and deserialize your data into and from a simple byte array.
Usually, you would use the SerializationUtility class, which wraps the serialization system and handles all the boilerplate code involved. Some example code might be as follows:
// Take a class with some random garbage string data, random numbers, unity objects, and cyclic references
public class MyData
{
public string str = new string(Enumerable.Range(0, 20).Select(i => (char)UnityEngine.Random.Range(50, 150)).ToArray());
public List<float> numbers = new List<float>(Enumerable.Range(0, 10).Select(i => UnityEngine.Random.Range(0f, 100f)));
public GameObject unityObjectReference = UnityEngine.Object.FindObjectOfType<UnityEngine.GameObject>();
public MyData reference;
}
// Somewhere, a method to serialize data to json might look something like this
private void SerializeData()
{
// Save to Assets folder
string path = Application.dataPath + "/data.json";
// Initialize some data
var originalData = new MyData();
originalData.reference = new MyData();
originalData.reference.reference = originalData;
// Unity should be allowed to handle serialization and deserialization of its own weird objects.
// So if your data-graph contains UnityEngine.Object types, you will need to provide Odin with
// a list of UnityEngine.Object which it will then use as an external reference resolver.
List<UnityEngine.Object> unityObjectReferences = new List<UnityEngine.Object>();
//DataFormat dataFormat = DataFormat.Binary;
DataFormat dataFormat = DataFormat.JSON;
//DataFormat dataFormat = DataFormat.Nodes;
// Serialization
{
var bytes = SerializationUtility.SerializeValue(originalData, dataFormat, out unityObjectReferences);
File.WriteAllBytes(path, bytes);
// If you want the json string, use UTF8 encoding
// var jsonString = System.Text.Encoding.UTF8.GetString(bytes);
}
// Deserialization
{
var bytes = File.ReadAllBytes(path);
// If you have a string to deserialize, get the bytes using UTF8 encoding
// var bytes = System.Text.Encoding.UTF8.GetBytes(jsonString);
var data = SerializationUtility.DeserializeValue<MyData>(bytes, dataFormat, unityObjectReferences);
}
}
This code results in a json file looking something like this:
{
"$id": 0,
"$type": "0|Test.SomeMonoBehaviour+MyData, Assembly-CSharp",
"str": "^|u\u008c\u0093l;L]\u008bS7\u0095\u00902?t74\u0090",
"numbers": {
"$id": 1,
"$type": "1|System.Collections.Generic.List`1[[System.Single, mscorlib]], mscorlib",
"$rlength": 10,
"$rcontent": [
43.22428,
24.3502026,
97.77544,
84.8498459,
29.8572845,
39.2639351,
3.33006382,
94.28071,
20.3551655,
38.9832077
]
},
"unityObjectReference": $eref:0,
"reference": {
"$id": 2,
"$type": 0,
"str": "Ii6Zet3Jb\u008c\u0084\u0085EQv\u0093j{[F",
"numbers": {
"$id": 3,
"$type": 1,
"$rlength": 10,
"$rcontent": [
19.2796345,
64.121666,
37.64587,
21.2250347,
73.44544,
40.8876953,
73.282135,
50.79189,
59.83468,
99.1492844
]
},
"unityObjectReference": $eref:0,
"reference": $iref:0
}
}
As you can see, Odin's json format keeps a bunch of metadata in special $-prepended entries. As such, this is not strictly completely correct json, but these are necessary to be able to fully reconstruct the data to the specifications of Odin's serialization system.
Whether or not you should use this, depends on your exact use-case though. If you just want to save some very simple data to json, we would advice you to use Unity's own JsonUtility class - it's a lot faster than Odin's json serialization, which is comparatively slow. (Odin's binary format is very fast and highly optimized - the json, not so much. We just never took the time to do it, as it wasn't a high priority. Json is never used in Odin, by default.)