Unity3d want to give up www, look at the new network request way Unitywebrequest__unity3d

Source: Internet
Author: User
Tags gettext

Sun Guangdong 2016.10.11

http://blog.csdn.net/u010019717

Unity will gradually abandon the WWW network request API, the new API request approach:

Unitywebrequestthe


The old www:

Https://docs.unity3d.com/ScriptReference/WWW.html

The new unitywebrequestthe:

Https://docs.unity3d.com/Manual/UnityWebRequest.html

Https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.html


Using Unityengine;
Using UnityEngine.Experimental.Networking;
Using System.Collections;
 
Class Mybehaviour:public Monobehaviour {=
    void Start () {
        startcoroutine () (GetText ());
    }
 
    IEnumerator GetText () {
        using (unitywebrequest www = unitywebrequest.get ("http://www.my-server.com")) {
            yield return Www.Send ();
     
            if (www.isError) {
                Debug.Log (www.error);
            }
            else {
                //Show results as text
                Debug.Log (www.downloadHandler.text);
     
                Or Retrieve results as binary data
                byte[] results = www.downloadHandler.data;
            }
        }
    }


This is the API provided in Unity5.4, in place of www to meet today's HTTP communication needs. Look at the official document, Unity5.2, 5.3, 5.4.


Unitywebrequest Architecture

The unitywebrequest is made up of three elements.

◾uploadhandler objects that process data to send data to the server

◾downloadhandler objects that receive data from the server

◾unitywebrequest is responsible for HTTP traffic control to manage objects of the top two objects.

To illustrate the relationships between these objects, as shown below.



Basic usage

Compare the basic usage of unitywebrequest and WWW classes.

Get

www through the wording of the URL:

Using Unityengine;
Using System.Collections;
 
Class Mybehaviour: Public  monobehaviour {
    void Start () {
        startcoroutine (GetText ());
    }
 
    IEnumerator GetText () {
        www request =  new www ("http://example.com");
 
        yield return request;
 
        if (! string. IsNullOrEmpty (Request.error)) {
            Debug.Log (request.error)
        } else {
            // 
            if ( Request.responseHeaders.ContainsKey ("status") &&
                    request.responseheaders["status" = =) {
                //
                string text = Request.text; 
                byte [] results = Request.bytes}}}


Change to unitywebrequest writing style:

Https://docs.unity3d.com/Manual/UnityWebRequest.html


Using Unityengine;
Using System.Collections;
Using UnityEngine.Experimental.Networking; 
//using Unityengine.networking;
 
Class Mybehaviour: Public  monobehaviour {
    void Start () {
        startcoroutine (GetText ());
    }
 
    IEnumerator GetText () {
        unitywebrequest request = Unitywebrequest.get ("http://example.com"); 
        //Unitywebrequest request = new Unitywebrequest ("http://example.com"); 
        //Request.method = Unitywebrequest.khttpverbget; 
        yield return request. Send (); 
        if (request.iserror) {
            Debug.Log (request.error);
        } else {
            if (Request.responsecode = = 200) {
                // 
                string text = Request.downloadHandler.text; 
                byte [] results = Request.downloadHandler.data}}}


See your document for more content.




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.