Unity C# 网络学习(十)——UnityWebRequest(一)
UnityWebRequest与WWW的区别文章来源地址https://www.toymoban.com/news/detail-401064.html
- UnityWebRequest将各种资源进行了拆分
- UnityWebRequest比WWW的效率更高,有很低的GC产生
- 更方便的上传数据
一.UnityWebRequest类获取数据
1.下载文本和二进制数据
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);
}
}
2.下载图片数据
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);
}
}
3.下载AssetBundle数据
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);
}
}
4.下载音频数据
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);
}
}
二.UnityWebRequest类上传数据
1.上传数据类MultipartFormDataSection
//======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]));
2.上传数据类MultipartFormFileSection
//======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"));
3.Post发送数据相关
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);
}
}
文章来源:https://www.toymoban.com/news/detail-401064.html
到了这里,关于Unity C# 网络学习(十)——UnityWebRequest(一)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!