WWW resource download and form submission

Source: Internet
Author: User
Tags call back

In the game of registering and verifying user information, as well as non-instant communication, we can use the WWW class to use short links to complete client and server data communication, today we will use the Post method to complete the user registration and login, at the end of the introduction of other resources loaded.

First use post to complete the registration:
The two Inputfield in the scene are used to enter a name and password, and a button submits the registration.
The button binding method is as follows:

1 usingUnityengine;2 usingSystem.Collections;3 usingUnityengine.ui;4  5  Public classRegisterandlogin:monobehaviour6 {7     Private stringRegisterurl;8     PrivateInputfield _name;9     PrivateInputfield _pwd;Ten       One     voidAwake () A     { -_name = Gameobject.find ("Name"). Getcomponent<inputfield>(); -_pwd = Gameobject.find ("PWD"). Getcomponent<inputfield>(); theRegisterurl ="http://..../wwwRegister.php"; -     } -   -      Public voidOnclickregister () +     { -         //define the fields that the Wwwform class uses to hold the post +Wwwform Registerform =Newwwwform (); A         //add the corresponding field to the form, and note that the key is consistent with the server atRegisterform.addfield ("name", _name.text); -Registerform.addfield ("Password", _pwd.text); -         //The Association starts to register - Startcoroutine (Registerfun (Registerurl, Registerform)); -     } -   inIEnumerator Registerfun (stringURL, wwwform form) -     { toWWW regi_www =NewWWW (URL, form); +         yield returnregi_www; -         //Get server return information the         stringCallBack =Regi_www.text.Trim (); *         //server returned "1", Registered successfully $         if(Callback.equals ("1"))Panax Notoginseng         { -Debug.Log ("congratulations! Register ok! (Call back code:"+ CallBack +")"); the         } +         //otherwise failed A         Else the         { +Debug.Log ("Register failed! (Call back code:"+ CallBack +")"); -         } $     } $}

Running the scenario, you can see that the registration succeeded as follows:

Duplicate registration results in failure:

Data is already in the database:

Login and note Similar, just need to judge on the server, and return information on the line, here is not written.

And then use to read a texture Picture:

1 voidStart ()2 {3 stringUrltexture ="Https://www.baidu.com/img/bd_logo1.png";4 Startcoroutine (Loadtexture (urltexture));5 }6  7 //load a picture texture8IEnumerator Loadtexture (stringURL)9 {Tenwww www =NewWWW (URL); One yield returnwww; A  This. Renderer.material.mainTexture =www.texture; -}

Finally, use get to read a JSON:
After querying the data on the server on request, the format is as follows:
[{"id": "1″," "Name": "\u5f6c\u5f6c", "Sex": "\u5973″," Age ":" 19″},
{"id": "2″," "Name": "\u8d85\u8d85″," Sex ":" \u7537″, "Age": "24″},
{"id": "8″," "Name": "\u5a01\u5a01″," Sex ":" \u7537″, "Age": "20″}]
Script code:

1 usingUnityengine;2 usingSystem.Collections;3 usingLitjson;4  5  Public classWwwscript:monobehaviour6 {7     //JSON template class8      Public class Person9     {Ten          Public stringID; One          Public stringname; A          Public stringsex; -          Public stringAge ; -     } the   -     voidStart () -     { -     //Specify request content with "? + Key-value pair" +Startcoroutine (LoadText ("Http://.../wwwGet.php?tablename=person")); -     } +   AIEnumerator LoadText (stringURL) at     { -www www =NewWWW (URL); -         yield returnwww; -Debug.Log ("loaded total data:"+www.text); -             //JSON processing -person[] persons = Jsonmapper.toobject <Person[]>(www.text); in         foreach(Person iteminchpersons) -         { toDebug.Log (item.id + item.name +item.age); +         } -     } the}

OK, I believe that through the above example, we can certainly extrapolate, to deal with the vast majority of WWW data submission and request problems ^_^

WWW resource download and form submission

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.