In this article, we’re going to create our leaderboard to host on a web server using very simple PHP and a little SQL. We’ll then make a Unity example in C# to add new scores and then display the top 20 scores.

OK, this is our first PHP file (settings.php), it’s used for saving the information about the database.

Here our database’s name is “testdb”, so let’s create it.

create_db

Alright, here our database is created. Now let’s insert a table to save players and scores. (setup.php)

Here “localhost:3306” is the server address and the port of MySQL, nothing difficult. Now let’s do the saving and fetching. (leaderboard.php)

This is the main part of our leaderboard. If there’s a field called “submit” in our request, then we insert a player & score pair into the table; otherwise we fetch the top 20 scores and display it.

So far so good, now let’s start up our Unity Editor and generate some real data. (Leaderboard.cs)

In the Upload function, we generate players and their score by random and send them to our PHP with the field “submit”. In the Download function, we just call our PHP and display the result.

In my example, I dropped 2 buttons and bound them to each function. Run the Unity scene and click the Upload button.

 result_mysql   result_page

As we can see, MySQL got our data and leaderboard.php displayed them correctly.

result_unity

When we click the Download button, our data is brought back correctly, as XML. If we are developing a real video game, all we have to do is parsing it into parts and bind them to GUI for each, and that is beyond the scope of this article.