Here, I would like to save three boolean arrays that specify if I have already picked up corresponding objects in my game. See the following:
![8844094–1205341–J(O_0@U6DW_8ZTKVK2OPEU.png|833x109
How can I do this using PlayerPrefs combined with JsonUtility?
What did you try? There’s plenty of documentation on this.
The more data you have, and the more types of data you have, the less PlayerPrefs makes sense to use. You can convert the bools to ints and even combine multiple bools into an int for efficient storage, but it’s going to be fragile, difficult, and inscrutable.
Better to look up a tutorial on how to save objects to JSON and write that data to a file in your Application.persistentDataPath. It’s actually really straightforward, far easier than trying to twist PlayerPrefs into doing something it was never designed to do. (What was it designed to do? Look at the name.)
Two notes based on the snippet of code we see here:
- You’ll have to store this data in an object - JSON serializers generally deal with objects and not static variables. Further, this object shouldn’t be a MonoBehaviour, but just a standalone data class solely meant to store save-able data. You can do this without making a ton of structural changes with a singleton:
public class YourSaveData
{
private static YourSaveData _instance;
public static YourSaveData Instance {
get {
if (_instance == null) _instance = new YourSaveData();
return _instance;
}
}
public bool[] weaponsPickedUp = new bool[9];
// and so on
}
//to access it
YourSaveData.Instance.weaponsPickedUp[0] = true;
- The builtin JsonUtility doesn’t handle collections well; use a third party library like the free Json.NET.