- Home /
Is there a need to check saving of PlayerPrefs
I am storing simple user data through PlayerPrefs and I was wondering if there was any need to check that the saved was successful before carrying on? For example:
PlayerPrefs.SetInt("KeyName", 10);
PlayerPrefs.Save();
if(PlayerPrefs.HasKey("KeyName")){
//Do action here
} else {
//throw error
}
P.S. I do understand that the PlayerPrefs save before exiting too.
Just FTR. PlayPrefs is unusable in the real world.
You have to use Secured PlayerPrefs (asset store), or write your own encrypted prefs.
It's one of the few things Unity has not yet had a chance to implement, that can't be used in the real world.
Don't always blame Unity ;) PlayerPrefs (which stands for Player Preferences) is actually ment to store preferences like volume level, player name, language, ..., other preferences. Since that's all just "data" you can use it to store arbitrary data, but it was never designed to be "secure".
No no no .. I don't blame them. it works perfectly.
What I meant was:
a) you can't do any real project with (somewhat) secure ability to store certain settings, etc.
b) Unity, has not, as yet, included this feature.
PlayerPrefs is great and will always be needed for one thing or another.
But you can't publish real-world games without obfuscated persistent info. It's that simple. So, you must use "Secured PlayerPrefs" from the store, or, write your own.
I'm sure in the futcha, U will cover this need.
Answer by Bunny83 · May 24, 2013 at 07:50 AM
Just use PlayerPrefs.Save to enforce a write to disk. However keep in mind to use this function sparely since it has to handle the IO stuff which could slow down your application when invoking every frame.
$$anonymous$$y question was not 'how' to save, but if a check to see that the save was successful is needed.
When the save function returns the playerprefs have been saved. If it can't save it it would throw an exception, either on SetInt or on Save. Just take a look at the PlayerPrefs documentation.
Your answer
Follow this Question
Related Questions
Can I transfer the PlayerPrefs.plist file to the iPhone? 1 Answer
Saving Scenes and loading GameObjects? 4 Answers
How to efficiently hand out rewards based on the level that the player completed? 3 Answers
Saving & Loading the scene (or at least one array) via Javascript 1 Answer
Timer that can count Hour difference between two DateTime for android 1 Answer