A quick and simple example(high score board)

The topic came up in Skype today, and I didn’t really finish the high scoreboard in a game I was working on recently, so I’ve finnished up and pulled together a few little code segments into a fairly vanilla script for Unity.

So first off, if you didn’t know already, Unity lets you store and access ints, floats, and strings on the hard drive using PlayerPrefs. More than enough for a quick highscoreboard. Better yet, since the PlayerPref keys are different for each game you can keep the hard coded names when you reuse the script. (It also means you can’t access those PlayerPrefs anymore if you change the name of your game.)

So now we should have a bunch of strings that should at least have a useable default value if not actual highscore data. Most importantly we’ve just defined the scores need to be stored; number first, followed by the name and separated by the Record Separator character.

Let’s assume the highscore table is all jumbled up (damn gremlins!) and we want to sort the scores so that the names/names are in correct order. It’s a short list so we’ll use bubble sort. Here’s a serious explanation and a cool video.

This sorts out the scores into ascending order (lows-left). So we have some scores being read and sorted. This example took a little longer than I expected (doesn’t everything?) so I’ll wrap up with the GUI code and the Add-Score code later.