PlayerPrefs keys

Is it possible get all the used PlayerPrefs keys?

Not that I know of. What are you trying to accomplish? Likely there’s a better way.

EDIT: If you have a list of the possible keys used, you can iterate through them and use the PlayerPrefs.HasKey method.

Given that its just a hash of data, having the keys is useful so that you can serialize or iterate over everything in the PlayerPrefs. Not having access to the keys but being able to touch everything is is… weird.

1 Like

Its no hash at all actually.
Playerprefs are stored as distinct objects in a system native form.

on iOS playerprefs is NSUserDefaults for example, on windows it ends in registry, no idea what osx does, likely plist, android uses androids data store, …

If you need to know all keys you have in (if you do, you missuse playerprefs for something you shouldn’t use it out of my view, its not really performant and I would never recommend to use it for gamesaves! On iOS its an easy way to cause problems as NSUserDefaults are loaded by iOS on app start and will remain permanentely in memory till you quit), you should perhaps use System.IO and serialize a dictionary (search for SerializableDictionary with google) to XML or compressed xml.

if you want to remain “lazy” store it as , seperated string or alike in a specific string key and split out the various keys from there later on again

Thanks for all your answers. I will shortly explain why I am not lazy about that :slight_smile: and why it would be extremely practicle in my situation.
At the very beginning of my game, the player can select a slot in which the progress is stored. Everything that is stored in that slot X gets a key that starts with "Slot X ". The player should be able to clear a slot, so I need to delete all the keys starting with "Slot X ". It would be extremely easy for me, if I could just iterate over all the keys and just delete the ones that start with “Slow X”.
If I could do it loke that, there would be no issues about forgetting to delete something. That’s the reason why I would like to do it, but as expected there is no way to do it.

Is this essentially what you’re doing or want to achieve?

private static string[] SavedProperties = new string[] {"Slow", "Health", "SomethingElse"};

private void ClearSave(int saveNumber)
{
	foreach(string savedProperty in SavedProperties)
	{
		PlayerPrefs.DeleteKey("Slot " + saveNumber + " " + savedProperty);
	}
}

FizixMan, that’s what I tried to avoid :-). You are certainly right that it can easily be done like that. My idea was basically that if something is forgotten or there is a bug in that part of the code, it is extremely hard to find it. So if I could just iterate over all keys there would be fewer potential problems. Of course it is not that hard to implement it as you described, but the other one would always work.
It is also a little bit more work, but acceptable :-).

I noticed PlayerPrefs can be very slow indeed, especially on Android. I tried saving about 800 values, and this took about a minute!
We’ll probably have to serialize a hash map.

I wonder if a file is created for every value in PlayerPrefs… We’ll probably go for a serializable dictionary, although I preferred the PlayerPrefs because then I don’t need to know where to save my files exactly (so our library code remains platform independent).