Want to watch this Lesson?

Start your FREE 7-day trial for instant access to all CG Cookie training! It takes 2 minutes and you can cancel anytime.

Start your Free Trial

Inserting Data and Final Setup

  • Difficulty:Intermediate

In this course I would like to show you how to establish a database connection from Unity to create a online scoreboard system, where everyone can insert and query scores from inside your game. If you want to try out the final result, you can check it out under: http://alexbilz.com/Unity/

This course contains 3 sections

In this first part you will learn how set up a web server. I will show you step by step how to get XAMPP, which is a local web server that allows you to follow this tutorial without spending extra money for a webserver. After the installation process is finished we will jump right into setting up the database, this process will contain setting up the the table spaces, the columns and importing some example data to play with. The Example Data csv- file can be found in the Example-Data folder of this zip folder. In the second lesson you will learn how to query data from your database. We will start with setting up the backbone part, which is basically a php script which establishes a connection to the database that we’ve created in the first video part. The results of this query will be returned as an array which we need to print row by row to get a readable format. In Unity we will use the WWW-Function to call the PHP Script (Display.php). The data that we’ve queried will be inserted into a scrollable text box. In the third part you will learn how insert data in the database. We will start with setting up the backbone part. This php scripts basically consists out of four functions:

  • -the mysqli_connect() function which allows us to connect to a database
  • -the mysqli_connect_errno() function to check if there were any errors during the connection process
  • -the mysqli_query() function to query the “INSERT”-SQL statement
  • -at the end we will close the database connection by using mysqli_close()
We will use PHP_POST[] as this is the currently safest method to send data the player name and score from a client to a web server as the data will be encoded. Beside the PHP scripting will also continue working in Unity itself, we will create a bunch more GUI Components to allow the user to add his name to the database and enhance the overall design with a GUI skin from the Unity Assets store. This .zip Folder contains everything you need to follow the first part of the tutorial series. Unfortunately I can’t provide a copy of XAMPP, but you can get a free copy under the following link: https://www.apachefriends.org/index.html