Unity3D network request notes, unity3d requests

Source: Internet
Author: User

Unity3D network request notes, unity3d requests

The methods of the Unity script for network requests are as follows:

Public WWW (string url, byte [] postData, Dictionary <string, string> headers)

Public WWW (string url, byte [] postData, Hashtable headers) -----> deprecated

Public WWW (string url, byte [] postData)

Public WWW (string url, WWWForm form)

Public WWW (string url)


There are many methods, as shown in the document.

However, when I was reading a book, I found that there was a method that had been abandoned. So I wrote down my notes to enhance my memory.


Refer to the Unity 3D/2D mobile game development book.

However, unfortunately, when Unity5 was used up, my ideas will be recorded based on the book.


1. Create a script, select C #, and name it WebManager.


2. Add the script WebManager. cs to an object to trigger the script event.


3. Start coding.


3.1 write an interface first.

Using UnityEngine; using System. Collections; public class WebManager: MonoBehaviour {// global variable, used to receive information prompts. The Initialization is "Nothing ". String m_info = "Nothing"; void OnGUI () {GUI. beginGroup (new Rect (Screen. width * 0.5f-100, Screen. height * 0.5f-100,500,200), ""); // create a label, set the position and size, and use the global variables that receive information prompts as the text content of the label. GUI. Label (new Rect (10, 10,400, 30), m_info); // create a button and set the position and size. The title of the button is "Get Data ". If (GUI. Button (new Rect (10, 50,150, 30), "Get Data") {// write the action and event triggered by clicking the Button here .} // Create a button and set the position and size. The title of the button is "Post Data ". If (GUI. Button (new Rect (10,100,150, 30), "Post Data") {// write the action and event triggered by clicking the Button here .} GUI. EndGroup () ;}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update (){}}



First, let's look at a simple method.

Get requests are the simplest, so generally simple methods are Get methods.


3.2Get Method

Write down the IGetData () function first. It should be noted that the return type of this function is IEnumerator, which can be called collaboratively.

IEnumerator IGetData () {// use Get to access the HTTP address WWW www = new WWW ("http://yococoxc.vicp.cc: 9999/test/userprint. php? Username = yococo & password = 123456789 "); // wait for the server's response yield return www; // if an error occurs if (www. error! = Null) {// get the server error message m_info = www. error; yield return null;} // get the server response text m_info = www. text ;}



Then you need to set the above function on the Get button for triggering.

if (GUI.Button (new Rect (10, 50, 150, 30), "Get Data")) {StartCoroutine(IGetData());}
Note:

The StartCoroutine () method is used to start the coprocessor and run the specified method. Of course, the returned type of the executed method must be IEnumerator.


The PHP code used by the program will be listed at the end.


Effect:



3.3Post Method

The Get method is as simple as above. The next step is the Post method, which is similar, but troublesome. Of course, the Post method has many advantages, which you know by default.

IEnumerator IPostData () {Dictionary <string, string> headers = new Dictionary <string, string> (); headers ["Content-Type"] = "application/x-www-form-urlencoded "; // string data = "username = yococo & password = 123456789"; // convert the text into a byte array byte [] bs = System. text. UTF8Encoding. UTF8.GetBytes (data); // submit the Post data WWW www = new WWW ("http://yococoxc.vicp.cc: 9999/test/userprint to the HTTP server. php ", bs, headers); // waiting for the server Yield return www; // if an error occurs if (www. error! = Null) {// get the server error message m_info = www. error; yield return null;} // get the server response text m_info = www. text ;}


Note:

If the Dictionary class is used, introduce using System. Collections. Generic; otherwise, an error occurs.


Then, the button corresponds to the method executed.

if (GUI.Button (new Rect (10, 100, 150, 30), "Post Data")) {StartCoroutine(IPostData());}

Result:



3.4 disposal method.

Public WWW (string url, byte [] postData, Hashtable headers)

Abandoned, this programming is not uncommon, there will be a relatively alternative method of disposal, documentation is the key.


3.5 use another Post method to implement another method.

IEnumerator IPostData () {// The Post text content to be sent string data = "username = yococo & password = 123456789"; // convert the text into a byte array byte [] bs = System. text. UTF8Encoding. UTF8.GetBytes (data); // submit the Post data WWW www = new WWW ("http://yococoxc.vicp.cc: 9999/test/userprint to the HTTP server. php ", bs); // wait for the server's response yield return www; // if an error occurs if (www. error! = Null) {// get the server error message m_info = www. error; yield return null;} // get the server response text m_info = www. text ;}
Header information is missing.

IEnumerator IPostData () {WWWForm form = new WWWForm (); // Add a field (Key, value) form. addField ("username", "yococo"); form. addField ("password", "123456789"); // submit the Post data to the HTTP server, submit the form WWW = new www ("http://yococoxc.vicp.cc: 9999/test/userprint. php ", form); // wait for the server's response yield return www; // if an error occurs if (www. error! = Null) {// get the server error message m_info = www. error; yield return null;} // get the server response text m_info = www. text ;}


The following is the PHP code:

<?phpif (isset($_GET['username']) && isset($_GET['password'])) {echo "GET -> username is " . $_GET['username'] . " and password is" . $_GET['password'];} else if (isset($_POST['username']) && isset($_POST['password'])) {echo "POST -> username is " . $_POST['username'] . " and password is" . $_POST['password'];} else {echo "error";}?>


Author: Mu zicai
IOS developer club 232099237 is available. You can join here to discuss the issue. Because the group is small, please enter verification information.

Related Article

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.