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.