Save & load system : playerprefs can save to a .save file instead of registry ?

Hi yall, I’m trying to create a save & load system that will

  1. save the spawned movable objects position, rotation, scale & whether or not it was deleted from the scene.
  2. let the user input the file name that he wants to save the file as and in the load menu give him the list of saved files and he can choose the one he wants to load.

Now I was able to achieve the first requirement with playerprefs but for the second one I’m stuck. Is there a way to have such .save file instead of saving it to registry with playerprefs ?

or if anyone knows any tutorial that satisfies both of my requirements please suggest it to me.Thankyou.

PlayerPrefs is a very well-named class, in that it is useful for saving preferences. If you’re trying to save anything else with PlayerPrefs, you’re gonna have a miserable time. It really isn’t at all designed for saving any data more complicated than a single value.

There are many ways to save actual data structures to files rather than just one variable at a time. One of the most popular is JSON. Here’s a JSON tutorial.

Note for the future: That tutorial uses Unity’s builtin JsonUtility, which is fine and certainly a good starting point (and leagues better than using PlayerPrefs). However, be advised that JsonUtility has some limitations, most notably in that it doesn’t support a lot of common data structures such as Dictionary. If you start running into problems like that down the line, install the Newtonsoft Json.NET Unity package and use that instead. Using it is nearly identical to using JsonUtility, so you should be able to simply find/replace your JsonUtility code with the Newtonsoft code and you’ll be alright.

1 Like

Adding onto @StarManta 's excellent post above, there’s a cool bridge you can use that takes you nearly seamlessly from PlayerPrefs over to a JSON-formatted save file.

You basically create one of these objects, associated with a particular filename, and use it exactly as if it was a PlayerPrefs. You can even make the created reference static so it’s VERY similar in many ways.

It’s not an ideal save game system, but if you’ve gone a significant way down the PlayerPrefs hole already, it might be a handy bridge to get you out to writing to a file, and even having multiple different savefiles, all while only changing a little bit of your code.

I cannot believe how simple this was to use. I luckily was smart enough to already collect all PlayerPrefs usage in a “GameSettings.cs” class that i reference everywhere else, so all I had to do was CMD + F in that script and change “PlayerPrefs.” to the new variable name of the JsonPlayerPrefs class.
Thank you, so much!

1 Like