当前位置:网站首页>Unity C# 网络学习(十)——UnityWebRequest(一)
Unity C# 网络学习(十)——UnityWebRequest(一)
2022-06-26 14:47:00 【帅_shuai_】
Unity C# 网络学习(十)——UnityWebRequest(一)
UnityWebRequest与WWW的区别
- 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);
}
}
边栏推荐
- D - Face Produces Unhappiness
- 网上找客户经理办理股票开户安全吗??
- Oracle ASMM and AMM
- 【使用yarn运行报错】The engine “node“ is incompatible with this module.
- Numpy basic use
- Document 1
- Combat readiness mathematical modeling 31 data interpolation and curve fitting 3
- Oracle11g database import and export method tutorial [easy to understand]
- Pytoch deep learning code skills
- Combat readiness mathematical modeling 32 correlation analysis 2
猜你喜欢

Get the intersection union difference set of two dataframes

JVM 输出 GC 日志导致 JVM 卡住,我 TM 人傻了

710. random numbers in the blacklist

备战数学建模31-数据插值与曲线拟合3

Unity uses skybox panoramic shader to make panorama preview. There is a gap. Solution

Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法

权威发布 | 延安大学2022年教师岗位招聘公告

Complimentary Book Cognitive Control: how does our brain accomplish tasks?

Attention meets Geometry:几何引导的时空注意一致性自监督单目深度估计

一篇抄十篇,CVPR Oral被指大量抄袭,大会最后一天曝光!
随机推荐
View触摸分析
一个快速切换一个底层实现的思路分享
Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type
Is it safe to open an account by digging money? Is there any risk?
人的顶级能量从哪里获取?
同花顺注册开户安全吗,有没有什么风险?
feil_uVission4左侧工目录消失
使用 Abp.Zero 搭建第三方登录模块(二):服务端开发
Introduction to basic knowledge of C language (Daquan) [suggestions collection]
Numpy basic use
赠书 | 《认知控制》:我们的大脑如何完成任务?
The JVM outputs GC logs, causing the JVM to get stuck. I am stupid
710. 黑名单中的随机数
Where do people get their top energy?
The DOTPLOT function in the epidisplay package of R language visualizes the frequency of data points in different intervals in the form of point graphs, specifies the grouping parameters with the by p
Mark一下 Unity3d在Inspector中选中不了资源即Project锁定问题
VMware partial settings
集群中命令的执行过程
【云原生】 ”人人皆可“ 编程的无代码 iVX 编辑器
Optimizing for vectorization