PlayerPrefs.Save()?

I am learning how to save variable data and I noticed the documentation says “Writes all modified preferences to disk.” for the Save() function.

Does that means it will save all the data that were changed from the last time I load them using GetInt,GetString or GetFloat? Or is it safer to continue use the Set functions to ensure everything were actually saved?

1 Like

In the reference it says:

“By default Unity writes preferences to disk on Application Quit. In case when the game crashes or otherwise prematuraly exits, you might want to write the PlayerPrefs at sensible ‘checkpoints’ in your game. This function will write to disk potentially causing a small hiccup, therefore it is not recommended to call during actual gameplay.”

Calling any of the Get calls only return the values. The only way to write out to the PlayerPrefs is to call Set. The save, like the doc says, actually saves it to disk. So, if a crash occurs, any sets calls you made without a save will be lost. Does this help?

4 Likes

Correct me if I’m wrong, so the Set calls were only saved temporary when the application runs and they will only be saved automatically when the application quits safely or with a manually called save() function?

That’s right.

As an aside, don’t overuse PlayerPrefs. Pay attention to the name - “player preferences” are what it should be used for. It can also be used for other trivial persistent data, such as storing the level the player is up to or things like that. I’ve often seen it used for very much non-trivial things, though, like storing a level’s state as a (very long) string or implementing a data hierarchy by using preference names recursively (ie: a preference that saves a part of the name of another preference to look up…). That kind of thing leads to trouble.

In short, if you find yourself thinking that something feels clumsy to put into a player preference then you’re probably not using it as intended and there’s probably a better and simpler solution.

I see. Thanks for replying and answering my doubt!

Guys, Where is PlayerPrefs.Save() saved? I cannot seem to find it in the project pack, nor in locations like Application.dataPath or Application.persistentDataPath. The manual says it is saved to disk, but cannot find it locally either… Where should I look?

In windows PlayerPrefs are stored in the registry (Run regedit to open it)
HKEY_CURRENT_USER > Software > Unity > UnityEditor > [Used company name for the project] > [Game name]

1 Like

I hope HamFar figured it out on his/her own in the intervening years…

4 Likes

Nearly four years and the poor guy still was waiting for an answer…

6 Likes

hey this stuff definitely helped me

1 Like

I read this in doc then I thought this

Then I make a small script and opened my saved registry (for in-editor and build) but in two cases only calling the PlayerPrefs.Set is actually changing things on disk. So the statement that saves will only happen when unity close isn’t true anymore?

If you want more information let me know

Some stuff triggers when it loses focus. So if you are switching to your registry after the Set is called, a save may still be triggered at that point. As far as I know, this has been the case at least as far as I recall due to some platforms not calling OnApplicationQuit.

This makes sense, probably it’s the case. Thanks for share