A downloadable asset pack

This Unity project demonstrates how to set up a global leaderboard using a hosted database, PHP and Unity. This method is definitely not the best or safest method to do something like this, however for information like usernames and scores its fine. I would not recommend using this for storing users sensitive/personal information.

You can download the package, Unity sample project and external files [Database export and PHP files] below, however I recommend heading to the Github repository here and cloning it all at once.

All files are attached to this project and can be used for whatever you wish. All Unity scripts are commented to explain what is going on, PHP scripts not as much. If you have any troubles with this or have any questions about changing things or expanding on it, leave a comment below!

How Does It Work?

There are three main components to this. The first is having a database hosted somewhere. You can do this locally by downloading MAMP and using its built in phpMyAdmin function to create the database, or you can use any online web hosting service to do this, as most come with a database package built in. Secondly, you will want to have several PHP scripts that communicate with your database. These PHP scripts will be hosted alongside your database, allowing them to talk to one another. Your PHP scripts receive information from Unity as a "form" and uses that information along with SQL queries to talk to the database, and returning information to Unity. Lastly you need to set up Unity to communicate with your PHP scripts, which will allow you to add and update users to the database, as well as receive information about it, all done in editor.

Database Setup

You can create your own database to use, however the supplied database file DatabaseFile/unityleaderboard.sql matches what the SQL queries in the PHP scripts are looking for, so creating your own means also tweaking the PHP scripts. To use the supplied database, just use the "Import" function in whatever database manager you are using and you should be good to go!

PHP File Setup

The PHP files needed are supplied in PHPFiles and just need to be hosted wherever your database is. If you are using MAMP just place them in C:\MAMP\htdocs and nothing should need to be changed in Unity to make it work. Some PHP files may need their connection information or SQL queries changed to match your database settings due to mess ups in the import process, however it should be straight-forward enough.

Unity Setup

If you are using MAMP and not changing any database settings everything should be set up to run immediately. DatabaseManagement.cs is the core to the Unity side of things. Each method in this has a reference to a URL where its required PHP file should be hosted. If you are using MAMP you should be set up to go, if you are hosting these online then just change the URL to where you have those files hosted.

Download

Download
DatabaseLeaderboardTutorial.zip 83 MB
Download
GlobalLeaderboards.unitypackage 1.4 MB
Download
unityleaderboard.sql 1.5 kB
Download
Leaderboard.php
Download
NewUser.php 1,013 bytes
Download
SaveData.php 1 kB

Leave a comment

Log in with itch.io to leave a comment.