how to save a lot of character data?
I am making a Simulation Game that you can buy and foster a lot of insects which have their own data(for example: hunger value ). so I want to save these bugs' data, I have found a method using
" PlayerPrefs.SetString("bug",JsonUtility.ToJson("the bug's class")); " but that means every insects has a own file (I need to use a loop to save every bugs' data) and I have to record how many bugs are and what kinds of insects it is? then , when I load this game I have to instantiate every bugs(what kinds ,which prefabs ,how many?) and give them their own data. It's very complex . Does anyone have a better idea? Thanks in advance.
Answer by eses · Sep 01, 2018 at 10:59 AM
Hi @dnwsaa58
What you need is serialization - Start reading about serialization in Unity - Search / google for: "unity serialization tutorial" and you'll find many good videos / tutorials.
You could use JsonUtility to serialize/deserialize your data to JSON, like you already do, but instead of single bug, you can create a class that contains bugs and other data related to bugs, and serialize that to JSON instead.
Also, instead of PlayerPrefs, you can also choose to use C# File or StreamWriter/Reader classes to write to / read from text files.
You could also use C# Binary serialization (BinaryFormatter) to serialize your data to non-(human)readable format...
There are many good questions and answers here in Answers on this topic.
Your answer
Follow this Question
Related Questions
After following a save tutorial, it won't work! 0 Answers
Save and Load GameObject with Components like Scripts 0 Answers
How to save money 1 Answer
Save Data Android C# 0 Answers
Problem with saving player data 0 Answers