赞
踩
UnityWebRequest与WWW的区别
private IEnumerator LoadText() { UnityWebRequest unityWebRequest = UnityWebRequest.Get("http://192.168.1.103:8080/Http_Server/zzs.txt"); yield return unityWebRequest.SendWebRequest(); if (unityWebRequest.result == UnityWebRequest.Result.Success) { string text = unityWebRequest.downloadHandler.text; byte[] bytes = unityWebRequest.downloadHandler.data; Debug.Log(text); Debug.Log(bytes.Length); Debug.Log("文本下载完成!"); } else { Debug.Log("下载失败:" + unityWebRequest.result); } }
private IEnumerator LoadTexture() { UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture("http://192.168.1.103:8080/Http_Server/xxx.jpg"); yield return unityWebRequest.SendWebRequest(); if (unityWebRequest.result == UnityWebRequest.Result.Success) { //方式一 Texture2D tex2D1 = (unityWebRequest.downloadHandler as DownloadHandlerTexture)?.texture; //方式二 Texture2D tex2D2 = DownloadHandlerTexture.GetContent(unityWebRequest); image.texture = tex2D2; Debug.Log("图片下载完成!"); } else { Debug.Log("下载失败:" + unityWebRequest.result); } }
private IEnumerator LoadAb() { UnityWebRequest unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle("http://192.168.1.103:8080/Http_Server/photo.ywj"); unityWebRequest.SendWebRequest(); while (!unityWebRequest.isDone) { Debug.Log(unityWebRequest.downloadProgress); Debug.Log(unityWebRequest.downloadedBytes); yield return null; } if (unityWebRequest.result == UnityWebRequest.Result.Success) { //方式一 AssetBundle assetBundle1 = (unityWebRequest.downloadHandler as DownloadHandlerAssetBundle)?.assetBundle; //方式二 AssetBundle assetBundle2 = DownloadHandlerAssetBundle.GetContent(unityWebRequest); if (assetBundle1 != null) Debug.Log(assetBundle1.name); if (assetBundle2 != null) Debug.Log(assetBundle2.name); Debug.Log("图片下载完成!"); } else { Debug.Log("下载失败:" + unityWebRequest.result); } }
private IEnumerator LoadAudioClip() { UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip("http://192.168.1.103:8080/Http_Server/music.mp3", AudioType.MPEG); yield return unityWebRequest.SendWebRequest(); if (unityWebRequest.result == UnityWebRequest.Result.Success) { AudioClip clip = DownloadHandlerAudioClip.GetContent(unityWebRequest); audioSource.clip = clip; audioSource.Play(); Debug.Log("音频下载成功!"); } else { Debug.Log("下载失败:"+unityWebRequest.result); } }
//======MultipartFormDataSection======
//1.二进制字节数组
dataList.Add(new MultipartFormDataSection(Encoding.UTF8.GetBytes("zzs666")));
//2.字符串
dataList.Add(new MultipartFormDataSection("zzs666"));
//3.参数名,参数值
dataList.Add(new MultipartFormDataSection("Name","zzs"));
dataList.Add(new MultipartFormDataSection("Msg",new byte[1024]));
//======MultipartFormFileSection======
//1.二进制字节数组
dataList.Add(new MultipartFormFileSection(Encoding.UTF8.GetBytes("zzs666")));
//2.文件名,字节数组(常用)
dataList.Add(new MultipartFormFileSection("上传的文件.jpg",File.ReadAllBytes(Application.streamingAssetsPath +"/test.jpg")));
//3.字符串数据,编码格式,文件名(常用)
dataList.Add(new MultipartFormFileSection("zzs!zzs!zzs!",Encoding.UTF8, "zzsTest.txt"));
private IEnumerator UpLoad() { List<IMultipartFormSection> data = new List<IMultipartFormSection> { new MultipartFormDataSection("Name", "MrTang"), new MultipartFormFileSection("Unity上传的文件.jpg", File.ReadAllBytes(Application.streamingAssetsPath + "/test.jpg")), new MultipartFormFileSection("zzs!zzs!zzs!", Encoding.UTF8, "zzsTest.txt") }; UnityWebRequest unityWebRequest = UnityWebRequest.Post("http://192.168.1.103:8080/Http_Server/", data); yield return unityWebRequest.SendWebRequest(); if (unityWebRequest.result == UnityWebRequest.Result.Success) { Debug.Log("上传完成!"); } else { Debug.Log("上传失败!" + unityWebRequest.result + unityWebRequest.error); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。