- Unity has a standard load/save function called PlayerPrefs.
- It accepts only basic types such as integers and strings.
- There is also JSONUtility as a standard feature
- So complex types should be converted to strings with JSONUtility.ToJson() and stored in PlayerPrefs
- But JsonUtil has a trap.
- Unity’s JSONUtility serializes List
to {} - Valid as JSON so it can be read/written with FromUtility<List
>, but null reference error because the content is empty
- Valid as JSON so it can be read/written with FromUtility<List
- In the first place, you can’t serialize a List, let alone an Array.
- Specifications.
- Cannot serialize an Array directly, but can serialize an instance of a class that has an Array as a field
- JsonUtil.ToJson of class Foo with Vector3[] as a field succeeds, but JsonUtil.FromJson
fails to compile because the compiler does not understand how to convert to Foo. - FromJsonOverwrite(json, foo) on a previously created Foo instance foo will succeed.
- Unity’s JSONUtility serializes List
- I thought I could create a proxy class to mediate information, and collect and distribute information with getter/setter of properties, but it seems that getter/setter is not called during serialization, so I decided to call it explicitly. cs
This page is auto-translated from /nishio/セーブとロード using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I’m very happy to spread my thought to non-Japanese readers.