![]() |
VOOZH | about |
PlayerPrefs can only save numbers and strings. But what if you need to save a player's inventory, position, health and quest progress, then JSON solves this by converting complex data into text format. JSON (JavaScript Object Notation) is a text format that stores structured data. It looks like this:
JSON is simple, readable, and Unity can convert your C# classes to JSON automatically.
JSON is perfect when you need to save many different values as one file.
First, create a class that holds all the data you want to save.
[System.Serializable] is required. It tells Unity this class can be converted to JSON.
Convert your class object into a JSON string and save it.
JsonUtility.ToJson() converts your class into a JSON string. Then save that string using PlayerPrefs.
Retrieve the JSON string and convert it back to your class.
JsonUtility.FromJson<GameData>() converts the JSON string back into a GameData object.
Here's a complete save system for a player: