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