THIS CONTENT DOWNLOAD SHORTLY

Objective

Main objective of this post to give you an idea about Centralized Leader Board in Unity Used as Personal Web API.

 

 

Step 1 Back End

First of all you need to make a simple database wherein you can store the user name, score and unique id of the user. 

simple-database

 

Step 2 Web Service

Now, after the database is done you need to make the web service using which you can add and get the scores. It can be done by any scripting language. Here, the demo shown uses Rest Service in Code Igniter.

<!--?php defined('BASEPATH') OR exit('No direct script access allowed'); require APPPATH.'/libraries/REST_Controller.php'; class Score extends REST_Controller { 	 	function __construct() { 		parent::__construct(); 		$this ---> load -> library("encrypt");
		$this -> load -> model("score_model", "score", true);
	}
	
	public function get_get()
	{
		$list = $this->score->getScore();
		$responce['Status'] = "Success";
		$responce['ScorList'] = $list;
		$this->response($responce, 200);
	}
	
	public function get_friends_post()
	{
		$data = $this->get_data();
		$data['FriendList'] = explode(",", $data['FriendList']);
		$list = $this->score->getFriendScore($data['FriendList']);
		$responce['Status'] = "Success";
		$responce['ScorList'] = $list;
		$this->response($responce, 200);
	}
	
	public function add_post()
	{
		$data = $this->get_data();
		$list = $this->score->getScore();
		$count = count($list);
		//print_r($count);exit;
		$record = $this->score->get_by_id($data['ScoreID']);
		$count = 0;
		if($count > 10)
		{
			
		}else{
			if($data['ScoreID'] == "" || $data['Name'] == "")
			{
				$responce['Status'] = "Error";
				$responce['MessageInfo'] = "Score ID or Name not empty.";
				$this->response($responce, 200);
			}
			
			if(empty($record))
			{
				$id = $this->score->insert($data);
				if($id)
				{
					$responce['Status'] = "Success";
					$responce['MessageInfo'] = "Score Added successfully.";
				}else{
					$responce['Status'] = "Error";
					$responce['MessageInfo'] = "Internal error occured.";
				}
				$this->response($responce, 200);
			}else{
				if($record['Point'] < $data['Point']) 				{ 					if($this->score->update($data))
					{
						$responce['Status'] = "Success";
						$responce['MessageInfo'] = "Score Update successfully.";
					}else{
						$responce['Status'] = "Error";
						$responce['MessageInfo'] = "Internal error occured.";
					}
				}else{
					$responce['Status'] = "Success";
					$responce['MessageInfo'] = "Score lower to older score successfully.";
				}
				$this->response($responce, 200);
				
			}
		}
	}
}
?>

Get Json query using the ci code which you will require in Unity. You can check Json query in DHC plug-in in chrome browser. 

get-score-post

After you get the required data move on to Unity.

 

Step 3 Implementing Web Service in code

How to use this web service in your code to add or get data?

Add Score – Add the following code to your script and replace the api with your api. Here,
JsonObject plug-in is used for Json parsing, you can use any Json parsing plug-in supported in Unity.

/// <summary>
/// Adds Score to database
/// </summary>
/// <param name="name"></param>
/// <param name="id"></param>
/// <param name="score"></param>
/// <returns></returns>
privateIEnumerator AddScore(string name, string id, string score)
    {
WWWForm f =newWWWForm();
        f.AddField("ScoreID", id);
        f.AddField("Name", name);
        f.AddField("Point", score);
WWW w =newWWW("demo/theappguruz/score/add", f);
yieldreturn w;
if (w.error ==null)
        {
JSONObject jsonObject =newJSONObject(w.text);
string data = jsonObject.GetField("Status").str;
if (data !=null&& data.Equals("Success"))
            {
Debug.Log("Successfull");
            }
else
            {
Debug.Log("Fatel Error");
            }
        }
else
        {
Debug.Log("No Internet Or Other Network Issue"+ w.error);
        }
    }

Get score - Add the following code:

/// <summary>
/// Gets the score list form the database .
/// </summary>
/// <returns></returns>
privateIEnumerator GetAllScoreList()
    {
string url ="demo/theappguruz/score/get";
WWW w =newWWW(url);
yieldreturn w;
if (w.error ==null)
        {
JSONObject jsonObject =newJSONObject(w.text);
for (int i =0; i < jsonObject.GetField("ScorList").Count -1; i++)
            {
Debug.Log(jsonObject.GetField("ScorList")[i].GetField("Name").str +" : "+ jsonObject.GetField("ScorList")[i].GetField("Point").str);
            }
        }
else
        {
Debug.Log("No Internet Or Other Network Issue : "+ w.error);
        }
    }

console

Given above is the output of the code.

I hope you find this blog very helpful while working with Centralized Leader board in Unity used as Personal Web API. Let me know in comment if you have any question regarding Unity3D I will reply you ASAP.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.

I am professional game developer, developing games in Unity 3D and I am very passionate about my work.

face mask Belial The Demon Headgear Pocket Staff Magic