- An empty scene in the Unity3D game engine and Main Camera
- An JavaScript scripting file for posting data to PHP page
- An table named highscore in the MySQL database
- An Server that can work PHP programming language
First, we will add javascript file to Main Camera Inspector in the empty scene.
function send(data, username) { var form = new WWWForm(); form.AddField("action", "send"); form.AddField("score", data); form.AddField("username", username); var url = "http://localhost/unity/send.php"; var w = WWW(url, form); yield w; } send(999, "phpservisi");
Let's get PHP file:
mysql_connect("localhost","username","password"); mysql_select_db("database_name"); if($_REQUEST['action']=="send") { $score = $_REQUEST['score']; $username = $_REQUEST['username']; $query = "INSERT INTO `highscore` (username, score) VALUES ('$username', '$score')"; mysql_query($query);
From on now, we can send our score data to PHP file for saving database, when the game run. send function helps us to POST datas. username and score datas are used via JavaScript file in the Unity3D game engine. $score and $username variables came from JavaScript file as you have known. After this we just use
SQL query.