Unity3D uses Webservice to read the database

I believe that everyone who is engaged in Unity3D development, whether it is a stand-alone game or an online game, especially a slightly larger project, will use the database, but there are very few tutorials on how to connect to the database with Unity3D on the Internet. It is how Unity3D connects to the mysql database, and it is a direct connection method, that is, directly connect to the database on the Unity3D side and operate the database. This method has many drawbacks, which are not mentioned here, and we should also abandon this direct connection method.
So how does Unity3D connect to the database? Using Webservice, in layman's terms, Webservice allows you to call resources from other websites, such as the weather and maps displayed on the webpage, and of course data! Webservice is just a service, providing this service can be Asp.NET, JAVA or PHP. The following is the three-tier structure of Webservice:

 

Structurally, if Unity3D wants to get the data of the database, you can ask the Web to get it, and the operation of the database has been outsourced to the Web, so you don't need to worry about it. Yes, in theory things seem as simple as that. The operation of the database on the Web is completely no problem, provided that everyone knows Asp.net, Java or PHP. But the question is how does my Unity3D get data from the web? I was stuck at this step at the time, well, look at a detailed structure, and you will suddenly realize:

 

Yes, Unity3D is not available directly from the web. The web output must be in XML or JSON format so that Unity3D can successfully read the data. It is recommended that you use JSON format, forget XML. Because JOSN is more concise and lightweight. Of course, this part is not your job, just leave it to the Web (server-side) staff and tell them you want data in JOSN format! Unity3D is very easy to read JSON format data! However, it is not ruled out that there are some men who make the three-tier structure by themselves. If you are a rookie and want to operate the three-tier structure by yourself, then you have to have a systematic study~ So the operation of the Web on the database, and how the Web outputs I will not talk about the JSON format.

Next, let's talk about an example, this example is available on the Internet, I just complicate simple things, and the rookie absorbs it!

PHP is used in the example , and the output is in JSON format, because it is easier to talk about. The structure is as follows:

 

We omit the operation of PHP on the database, the following are the steps:

 

1. First create an index.php file with Notepad and write in it

<?php

$arr=array( //An array with two elements, name and sex

      'name' => 'Bill', //you can assume 'name' => 'select name from table where name="bill"

      'sex' => 'Male'

);

echo json_encode($arr); //The output is in JSON format

?>

After saving it, hang it on the Internet. We can use Apache or VertrigoServ as a PHP server test.

 

Next, all we have to do is display the elements of the array in Unity3D

 

2. Create a new project in Unity3D, create a new JS script named DB, and write in it

var url = "http://192.168.1.102/"; //This is my local address

private var showname; //Define two variables to accept the elements of the array in index.php

private var showsex;

function Start () {

var www : WWW = new WWW (url); //This part looks at the API of www, you know

yield www;

print(www.text); //You can see what is passed from index.php, easy to understand

var JsonData=eval(www.text); //eval() is used to calculate (parse) JSON, which is very useful

showname=JsonData["name"]; //Assign array elements to variables

showsex=JsonData["sex"];

}

function Update () {

}

 

 

function OnGUI () {

// Create a text field that the user can modify and edit.

GUI.TextField (Rect (10, 10, 200, 20), showname, 25); //Define two TextFields to display name and sex

 

GUI.TextField (Rect (10, 50, 200, 20), showsex, 25);

}

OK, so far, run, the output is:

 

As you can see, this is the content of www.text, and what eval() does is to parse the array elements in www.text!

 

Chinese output can refer to http://oulehui.blog.163.com/blog/static/79614698201123095952444/

For data submission, you can use the WWWfrom form API, refer to

http://www.cocoachina.com/bbs/simple/?t9880.html

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324731622&siteId=291194637