Save Score To Database

Step 1:Create Database in MySql

Step 2:Create Table Inside Unity Database

Step 3:Create addScore.php and place into htdocs folder

$db = mysql_connect("localhost", "root", "") or die('Could not connect: ' . mysql_error());
mysql_select_db("Unity") or die('Could not select database');
// Strings must be escaped to prevent SQL injection attack.
$name = mysql_real_escape_string($_GET["name"], $db);
$score = mysql_real_escape_string($_GET["score"], $db);
// Send variables for the MySQL database class.
$query = "insert into Score values (NULL, '$name', '$score');";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

Step 4:Create display.php and place into htdocs folder

// Send variables for the MySQL database class.
	$database = mysql_connect("localhost", "root", "") or die('Could not connect: ' . mysql_error());
	mysql_select_db("Unity") or die('Could not select database');
	$query = "SELECT * FROM Score ORDER by score DESC LIMIT 10";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	$num_results = mysql_num_rows($result);
	for($i = 0; $i < $num_results; $i++)
	{
     	$row = mysql_fetch_array($result);
     	echo $row['name'] . "\t" . $row['score'] . "\n";
	}
    if($num_results==0){
   	 echo "No Score Found";
    }

Step 5:Create MySQLController.CS

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MySQLController : MonoBehaviour
{
    private string addScoreURL = "http://localhost/addScore.php"; 
    private string getScoreURL = "http://localhost/display.php";
    public Text displayScoreText;
    public Text statusText;
    private static MySQLController hsControllerInstance;
    void Awake(){
        hsControllerInstance=this;
    }
    void Start(){
    }
    public static MySQLController Instance{
        get{
            return hsControllerInstance;
        }
    }
    public void SaveScore(string name,int score){
        StartCoroutine(PostScores(name,score));
    }
    public void GetScore(){
        StartCoroutine(GetScores());
    }
      IEnumerator PostScores(string name, int score)
    {
        string post_url = addScoreURL + "?name=" + WWW.EscapeURL(name) + "&score=" + score;
        statusText.text="Loading...";
       WWW hs_post = new WWW(post_url);
        yield return hs_post;         
        if (hs_post.error != null)
        {
            print("There was an error posting the high score: " + hs_post.error);
            statusText.text=""+hs_post.error;
        }else{
            statusText.text="Score Saving Successfull";
        }
    }    
    IEnumerator GetScores()
    {
        statusText.text="Loading...";
        WWW hs_get = new WWW(getScoreURL);
        yield return hs_get;   
        if (hs_get.error != null)
        {
            statusText.text=""+hs_get.error;
            print("There was an error getting the high score: " + hs_get.error);
        }
        else
        {
            displayScoreText.text = hs_get.text;    
        }
    }   
}

Step 6:Create ScorePanelUIScript.CS

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScorePanelUIScript : MonoBehaviour {
    public InputField nameInputfield;
    public InputField scoreInputfield;
    // Use this for initialization
    void Start () {   
    }  
    // Update is called once per frame
    void Update () {    
    }
    public void OnSaveScoreButtonClick(){
        int score=int.Parse(scoreInputfield.text);
        MySQLController.Instance.SaveScore(nameInputfield.text,score);
    }
    public void OnGetScoreButtonClick(){
        MySQLController.Instance.GetScore();
    }
}

Step 7:Create UI in Unity and Add Scripts

Step 8:Register UI Button Events

1.  Add GetButton Event

2.add Save Button Event

Let's Think together, Say Something !