I'm storing player name and scores in the Unity PlayerPrefs. I'd like to list all the names and scores stored in PlayerPrefs and display a high score page:

enter image description here

Is it possible to display all the keys values previously stored in the PlayerPrefs? Or can someone provide me by a JavaScript to do this?


It is not possible to get the names of the PlayerPrefs keys at runtime. Instead you could opt for serializing your highscore table, and adding it as a string value to a fixed key in PlayerPrefs.

This is untested C#, but you'll figure it out:

// Load highscores.
string highscoresSerialized = PlayerPrefs.GetString("highscores", "");
Dictionary<string,int> highscores = MyUnserialize(highscoresSerialized);

// Update highscores.
highscores["Xavier"] = 100;

// Show highscores.
foreach ( var entry in highscores )
    print(entry.Key + ": " + entry.Value);

// Save highscores.
string highscoresSerializedAgain = MySerialize(highscores);
PlayerPrefs.SetString("highscores", highscoresSerializedAgain);

MySerialize is a function that should turn a Dictionary<string,int> into a string (e.g., "Scott:100;Henry:200;Jean:150") and MyUnserialize should perform the reverse operation.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.