- Home /
PlayerPrefs: force *Load*?
Can I force Unity to reload PlayerPrefs from disk?
It is possible to force PlayerPrefs to be saved using PlayerPrefs.Save(), however, there is no equivalent for loading.
I have a WebPlayer game, and at certain times I want to synchronize between multiple instances in different windows - eg. if the player saves his game in one WebPlayer instance, I call PlayerPrefs.Save() to ensure it's on disk, but when in another WebPlayer instance he needs to Load, I need to ensure that PlayerPrefs are reloaded.
From testing, the WebPlayer ignores changes to the underlying file.
You could try get all the variables individually and see if that works. However, there is no force Load that I can find.
I'm not sure what you mean by getting all the variables individually. When I retrieve values (individually - is there any other way?), I do not get ones recently written to disk with Save() in another WebPlayer.
Answer by Bunny83 · Feb 06, 2012 at 11:19 PM
Well, i don't think that's possible. Interprocess communication was never the aim of PlayerPrefs. Sure, it would be nice to use it that way, but i guess you have to communicate over your hosting server. I don't see much alternatives.
Well, when hosted on the same domain (which would also be necessary for PlayerPrefs since it depends on the URL) you could try to go through the browsers Javascript with ExternalEval, find the other instance and use SendMessage of the UnityPlayer-plugin.
It might get a bit tricky to locate the other instance(s), but when you created the webpage you can always name the window / tab in a way it can be found by others.
At one time it was "never the aim" of Unity to work on Windows, and only $$anonymous$$acOSX was supported. I'll hope for Unity to add PlayerPrefs.Load() rather than make excuses for them.
;) absolutely agree on that. Save() has also been added afterwards. But i guess even when you file a feature request it's really one of the lowest priority requests (i only count the serious ones) since there are really just a few people that need this feature.
$$anonymous$$aybe reloading of the .unity3d file will also reload the playerprefs, but, of course, that would restart your game / app.
Just use WWW.LoadUnityWeb
That's one of the problems with democratic feature requests, it can obfuscate trivial requests like this. But yes, if they spent all day on trivial stuff, $$anonymous$$acOSX developers would have PlayerPrefs.Load already ;-)
Answer by HolBol · Feb 06, 2012 at 05:27 PM
I've had it update while the game is playing before, with no problem. Are you using PlayerPrefs.Load()? It might be what you are looking for. If not, then.. I'm not sure how to help you!
There is no "PlayerPrefs.Load()", only Save(), which is the problem.
Ahh. Can't you just use the GetInt, GetString, and GetFloat functions?
Would using SetInt, SetString, and SetFloat replace the Save function? No. The point is, if prefs are changed in another concurrent webplayer instance, they are not visible in the earlier-started instance.
Please, calm it a little. What I'd do is put in a feature request. They may have intentionally left it out for security reasons, however.
@Fishman92: That doesn't make much sense. The OP already said that "there is no equivalent for loading", so why do you suggest to use a function that doesn't exist? Also i can't even imagine any case where the reloading of the playerprefs could be a security issue.