HTTP Communication in unity3d

Source: Internet
Author: User
Tags php server

Preface

Unity3d is a cross-platform engine. unity3d is undoubtedly the most dazzling star in the mobile Internet wave, especially in the 3D direction of mobile games. Unity3d HTTP Communication is very simple and easy to use. The following uses HTTP and PHP
Sever interaction is a brief introduction.

Principles of HTTP data submission

HTTP uses a URL to obtain and submit data. There are two ways to submit data: Get and post. Get is generally used to tell the server to send back data that meets the parameters.

For example, the HTML code of get is as follows:

     <form action="search.php" method ="GET">         <username:<inputtype="text"name="user"/><br>         <password:<inputtype="password "name="pwd"/><br>          <input type="submit"value="login"/>     </form >

Post generally sends data to the server, and the server processes the data, such as storing it in the database.

For example, the HTML code of post is as follows:

     <form action="login.php" method ="POST" >         <username:<inputtype="text"name="user"/><br>         <password:<inputtype="password "name="pwd"/><br>          <input type="submit"value="login"/>     </form >

The difference is that the submission method is different. After you click the login button, the address bar of the browser is displayed as follows:


The get Method URL is http: // 127.0.0.1/serach. php? User = hortor & Pwd = 123


Post Method URL: http: // 127.0.0.1

PHP server receiving data method

The server responds to these two methods in two ways:

The get method is $ _ Get [user] to receive the user value sent by the client.

The method for receiving data by the POST method is $ _ post [user], which is also used to receive the user value of the client variable.

How to Use the unity WWW class

The Unity WWW class also corresponds to two data submission methods. The two commonly used constructors are:

Static FunctionWWW (URL:String): Www

Static FunctionWWW (URL:String, Form:Wwwform)
: Www

The first function is to send data to the server through the get method, and the second function is to submit data using the POST method. The wwwform method is addfield (AGR: String, value: string ), to add parameters.

Get example:

#pragma strictprivatevar url : String = "127.0.0.1/login.php?user=test&pwd=123";function Start () {     var getData : WWW = WWW(url);     yield getData;          if(getData.error != null) {          Debug.Log(getData.error);     }     else {          Debug.Log(getData.text);     }}

Post example:

#pragma strictprivatevar url : String = "127.0.0.1/login.php";function Start () {     var form : WWWForm = new WWWForm();     form.AddField("user", "test");     form.AddField("pwd", "123");     var getData : WWW = WWW(url, form);     yield getData;     if(getData.error != null) {          Debug.Log(getData.error);     }     else {          Debug.Log(getData.text);     }}

The yield method is called every frame by the program. When the getdata execution is complete, the result is returned, which is equivalent to the asynchronous request data. The WWW class has several common static variables:

1. www. Text returns the data from the webpage. The type is string.

2. www. Error returns an error message, such as timeout or network connection error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.