Editor PlayerPrefs getting repopulated with deleted values
I'm having an issue where I can't properly clear the PlayerPrefs under Mac OSX. I have delete the plist file located under ~/Library/Preferences/
, and the old values always comes back. I've tried closing the Unity editor completely and then deleting the file, but the values always comes back.
I've experienced this behavior with multiple keys from various source. In one case, old PlayerPrefs keys that were no longer in my source code were mysteriously showing up. In another, a plugin that pulled from PlayerPrefs was someone getting old values post PlayerPrefs deletion. This combined behavior makes me think that something in Unity is caching these old values, and since the behavior happened ever after an exit and restart, it makes me think Unity is caching PlayerPrefs to disk somewhere else beside ~/Library/Preferences/
.
Any idea what could be keeping the older values around?
Same here (5.4): I exit Unity and delete the plist from ~Library/Preferences, and yet values somehow come back! $$anonymous$$akes my title fail, and I cannot start fresh.
Seems to be platform independent as I noticed the same behaviour under Windows (7). Deleted registry keys are showing up out of the dark
I don't have time right now, but it would be great if someone could open a bug with Unity about this. The behavior is fairly easy to demonstrate with the BundleVersionChecker Plugin. Just create a new project, add the plugin, select a directory for the plugin output and delete the plist and the value should still exist.
Answer by AlexisDow · Aug 10, 2017 at 06:56 PM
I was also baffled to see that every time I changed the .plist file containing the PlayerPrefs, any value read from it would revert to the state of the last execution of my game, overwriting my changes in the file. Removing the file wasn't cutting it either.
But the culprit wasn't Unity, it was MacOS: all preferences are actually written through an Apple API and cached (probably in RAM), which makes manual manipulations of the .plist file impossible, unless maybe you reboot your computer or logout/login of your user session.
This got me on the way: http://hints.macworld.com/article.php?story=20130908042828630
If you want to change simple values (non-array, non-nested) , you can use defaults
from a terminal. This will change the value and update the cache.
defaults write unity.mycompany.mygame someKey -string 'some value'
For more complicated stuff, this will require a custom script using the CFPreferences
API from Apple.
Very useful! That may explain why I've had SO$$anonymous$$E luck quitting the editor before making changes. $$anonymous$$ight cause the cached data to be dealt with?
I think in this case it was just luck :) $$anonymous$$e trying to open/close the Editor didn't change a thing. Again, this has nothing to do with Unity, so you probably made a change just before a scheduled refresh of the cache by the OS. Again, I am just speculating here.
Answer by Naphier · May 13, 2016 at 06:35 AM
I always have an editor script that adds a menu item that calls a method calling PlayerPrefs.DeleteAll() saves the trouble of having to search for the prefs.
I did use a work around to resolve my particular issue. I'd still really like to know where these mysterious values are co$$anonymous$$g from.
Depends on what they are. Every time you run a build, Unity is saving some stuff to player prefs. On windows it is the window height/width and quality settings. Plus a couple other things I'm not sure of. So when in development I often have to reset player prefs when I change the dimensions of the exe's window.
I updated the question with more details about the behavior I am seeing. Should hopefully make it clearer.
PlayerPrefs.DeleteAll() does successfully clear the data, wherever it's being cached.
(In my case, I didn't bother with an editor script, just stuck that in my project, ran it, then commented it out once the bad prefs were gone.)
This sounds like a Unity bug. Unity should NOT transfer the PlayerPref values added by my game in the Editor into the build.
First of all, if we need to remember to manually clear the PlayerPrefs before each build for releasing a new update for our games etc, we're running the risk of forgetting it and having our new users inheriting whatever settings we had in our Unity project for testing. It's our responsibility to do our best to test everything before an update is released of course, but it's Unity's responsibility to provide optimal default behaviour to help with that. That would be to remove those PlayerPref values from the build by default.
On the other hand, I should be able to keep those PlayerPref values in my Unity project, not having to delete them just to exclude from the build if I don't want to.