Download and upload of resources--02 upload

Source: Internet
Author: User

Study Notes for beginners, please correct me if you have any mistakes. The number is also please point out, thank you.

Upload

When downloading resources from the server, we usually need to tell the server device information, user information and so on to download the corresponding resources

Parameters or files can be uploaded via the Unityengine.wwwform class as a www parameter

The device type can be obtained by application.platform, for example: Runtimeplatform.iphoneplayer or Runtimeplatform.android

Device memory size grams is obtained through systeminfo.systemmemorysize, for example: The server determines the size of the memory to return to the client normal or high-definition resources

The example shows how to use Unityengine.wwwform to simulate uploading data to the server , the process of the server to return the corresponding data according to the information, and the ability to upload user information and high score record data through Unityengine.wwwform.

1 Preparing the document "Info.text" as downloaded content

2 Create a new scene

3 Create a new game object and name it "Wwwmanager" to add the following script:

usingUnityengine;usingSystem.Collections; Public classC_13_2_1:monobehaviour {Private stringaddress; Privatewwwform form; voidAwake () {//AddressAddress ="file://"+application.datapath+"/chapter13/info.txt"; //Create wwwform and add information such as version numberform =NewWwwform (); Form. AddField ("version",1);//Game Version NumberForm. AddField ("username","John");//User nameForm. AddField ("Device", Application.platform.ToString ());//Device TypeForm. AddField ("Memory", systeminfo.systemmemorysize);//Device Memory Size    }        voidOngui () {if(Guilayout.button ("Load")) {            //use the co-process to downloadStartcoroutine (address); }} IEnumerator Load (stringURL)        {Debug.Log (URL); //upload form to URL address and download datawww www =NewWWW (Url,form); yield returnwww; stringText =Www.text;    Debug.Log (text); }}

The instance upload screen is as follows:

usingUnityengine;usingSystem.Collections;usingSystem.IO; Public classC_13_2_2:monobehaviour { Public stringScreenshoturl; voidOngui () {if(Guilayout.button ("Upload") {startcoroutine (Uploadpng ()); }} IEnumerator Uploadpng () {//wait until the current frame is rendered complete        yield return NewWaitforendofframe (); stringPath = Application.datapath +"/shot.png"; //screenshot and saveapplication.capturescreenshot (path); //Reading screenshot Pictures        byte[] bytes =file.readallbytes (path); //upload a picture using formWwwform form =NewWwwform (); Form. Addbinarydata ("FileUpload", Bytes,"Screenshot.png","Image/png"); WWW W=NewWWW (Screenshoturl, form); yield returnW; if(!string. IsNullOrEmpty (W.error)) {print ("There are errors:"+w.error); }        Else{print ("Upload completed"); }    }}

Download and upload of resources--02 upload

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.