赞
踩
仅下载文件存至本地推荐使用DownloadHandlerFile
public IEnumerator DownFile() { //下载路径 string url = ""; //存储的本地路径 string localurl = ""; UnityWebRequest WebRequest = new UnityWebRequest(url); DownloadHandlerFile Download = new DownloadHandlerFile(localurl); WebRequest.downloadHandler = Download; yield return WebRequest.SendWebRequest(); //等待资源下载完成 while(!WebRequest.isDone) { yield return null; } if(string.IsNullOrEmpty( WebRequest.error)) { //文件下载成功 } else { //文件下载失败 } }
读取图片推荐使用DownloadHandlerTexture
public IEnumerator DownTexture() { /* 读取的包外路径 当为安卓环境时需添加前缀 file:// 路径需要包含文件的后缀名 */ string url = ""; UnityWebRequest WebRequest = new UnityWebRequest(url); DownloadHandlerTexture Download = new DownloadHandlerTexture(); WebRequest.downloadHandler = Download; yield return WebRequest.SendWebRequest(); //等待资源下载完成 while(!WebRequest.isDone) { yield return null; } if(string.IsNullOrEmpty( WebRequest.error)) { //文件下载成功 //读取的图片 Texture2D rexture = Download.texture; } else { //文件下载失败 } }
读取文件推荐使用DownloadHandlerBuffer
public IEnumerator DownBuffer() { /* 读取的包外路径 当为安卓环境时需添加前缀 file:// 路径需要包含文件的后缀名 */ string url = ""; UnityWebRequest WebRequest = new UnityWebRequest(url); DownloadHandlerBuffer Download = new DownloadHandlerBuffer(); WebRequest.downloadHandler = Download; yield return WebRequest.SendWebRequest(); //等待资源下载完成 while(!WebRequest.isDone) { yield return null; } if(string.IsNullOrEmpty( WebRequest.error)) { //文件读取成功 //读取的数据 var data = Download.data; } else { //文件读取失败 } }
public IEnumerator Post_Demo() { //Post请求的地址 string url = ""; //Post请求的参数 WWWForm form = new WWWForm(); form.Add("key1","value1"); form.Add("key2","value2"); UnityWebRequest webRequest = UnityWebRequest.Post(url, form); //发送请求 yield return webRequest.SendWebRequest(); if(string.IsNullOrEmpty( webRequest.error)) { //Post的请求成功 //Post请求的返回参数 var data = webRequest.downloadHandler.text; } else { //Post的请求失败 } }
public IEnumerator Get_Demo() { //Get请求的地址 string url = ""; UnityWebRequest webRequest = UnityWebRequest.Get(url, form); //发送请求 yield return webRequest.SendWebRequest(); //等待请求完成 while(!webRequest.isDone) { yield return null; } if(string.IsNullOrEmpty( webRequest.error)) { //Get的请求成功 //Get请求的返回参数 var data = webRequest.downloadHandler.text; } else { //Get的请求失败 } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。