The Unity script has the following methods for network requests:
Public WWW ( string url, byte[ ] PostData, Dictionary<string, string > Headers )
public &NBSP; www Span style= "COLOR: #444444" > ( string url ,&NBSP; byte []&NBSP; postdata ,&NBSP; hashtable &NBSP; headers deprecated
public &NBSP; www ( string &NBSP; url ,&NBSP; byte []&NBSP; postdata )
< Span style= "COLOR: #009695" >public &NBSP; WWW ( string &NBSP; url ,&NBSP; wwwform &NBSP; form )
< Span style= "COLOR: #444444" >public &NBSP; www ( Span style= "COLOR: #3363a4" >string &NBSP; URL
Many ways, as you can see from the documentation.
But in the process of reading I found that a method has been discarded, so deliberately write down notes, enhance memory.
Refer to "Unity 3d/2d mobile game development" as a self-taught book.
But unfortunately in the Unity5 when there are methods to be discarded, my ideas will be based on the book to record.
1. Create a script, select C #, named Webmanager.
2. Add the script WebManager.cs to an object to trigger the scripted event.
3. Start writing code.
3.1 Write an interface first.
Using unityengine;using System.collections;public class Webmanager:monobehaviour {//global variable, used to receive informational hints, initialized to "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, size, and the global variable that will receive the message prompt as The text content for the label. Gui. Label (New Rect (Ten, ten, +), m_info);//Create a button, set the position, size, and the caption "Get Data" on the button. if (GUI. button (new Rect (), "Get Data") {//The behavior, events triggered by the Click button in this case. }//creates a button, sets the position, size, and the caption on the button is "Post Data". if (GUI. button (new Rect (+,-), (+), "Post Data")} {//write here the action, event of the click button triggered. }gui. Endgroup ();} Use the For Initializationvoid Start () {}//Update is called once per framevoid update () {}}
Let's start with a simple method.
The GET request is the simplest, so the general simple method is the Get method.
3.2Get method
Write down this igetdata () function first. It is important to note that the return type of the function is an iterator IEnumerator, which allows for cooperative invocation.
IEnumerator Igetdata () {//Use Get method to access http address www www = new www ("Http://yococoxc.vicp.cc:9999/test/userprint.php?username =yococo&password=123456789 ");//wait for the response of the server yield return www;//If an error occurs if (Www.error! = null) {//Get server error message M_info = Www.error;yield return null;} Gets the response text of the server M_info = Www.text;}
Then you need to set the above function on the get corresponding button so that it can be triggered.
if (GUI. button (new Rect (n, Max, max), "Get Data")) {Startcoroutine (Igetdata ());}
Description
The function of the Startcoroutine () method is to start a synergistic program, execute the specified method, and of course the return type of the method to be executed must be IEnumerator.
The PHP code used by the program will be listed at the end.
The effect is:
3.3Post method
The Get method is as simple as above, the next is the Post method, some similar, but more troublesome, of course, the Post method has many advantages, by default you know.
IEnumerator ipostdata () {dictionary<string,string> headers = new dictionary<string, string> (); headers [" Content-type "] =" application/x-www-form-urlencoded ";//The Post text content that will be sent string data =" username=yococo&password= 123456789 ";//Convert text to byte array byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes (data);//submit post data to HTTP server www www = new www ( "Http://yococoxc.vicp.cc:9999/test/userprint.php", BS, headers);//wait for the response of the server yield return www;//If an error occurs if (www.error! = NULL) {//Get server error message M_info = Www.error;yield return null;} Gets the response text of the server M_info = Www.text;}
Special tips:
Using the Dictionary class, introduce the using System. Collections . Generic ; otherwise it will go wrong.
The button then corresponds to the method that is executed.
if (GUI. button (new Rect (Ten, (+), (), "Post Data") {Startcoroutine (Ipostdata ());}
Results:
3.4 Method of disposal.
Public WWW (string URL, byte[] PostData, Hashtable Headers)
Is discarded, this programming is not uncommon, there will be a relative substitution of discarded methods, the document is the key.
3.5 additional post notation to implement another method.
IEnumerator Ipostdata () {//Post text content to be sent string data = "username=yococo&password=123456789";//Convert text to byte array byte[] BS = System.Text.UTF8Encoding.UTF8.GetBytes (data);//submit post data to HTTP server www www = new www ("http://yococoxc.vicp.cc:9999/ Test/userprint.php ", BS);//wait for the response of the server yield return www;//If an error occurs if (Www.error! = null) {//Gets server error message M_info = Www.error; yield return null;} Gets the response text of the server M_info = Www.text;}
Add header information is missing here.
IEnumerator Ipostdata () {wwwform form = new Wwwform ();//Add field (key, value) Form. AddField ("username", "Yococo"), form. AddField ("Password", "123456789");//submit post data to HTTP server, submit form www www = new www ("http://yococoxc.vicp.cc:9999/test/ userprint.php ", form);//wait for the server to respond yield return www;//If an error occurs if (Www.error! = null) {//Get server error message M_info = Www.error;yield return null;} Gets the response text of the server M_info = Www.text;}
The next step 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-and username is". $_post[' username '. "And password is". $_post[' password ');} else {echo "error";}? >
Muzi only
iOS Developer Club 232099237 free to join here to discuss the problem, because of the few, enter the verification information.
Notes on the Unity3d network request