当前位置:网站首页>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);
}
}
边栏推荐
- It's natural for the landlord to take the rent to repay the mortgage
- What is the ranking of Guosen Securities? Is it safe to open a stock account?
- Pytorch深度学习代码技巧
- [cloud native] codeless IVX editor programmable by "everyone"
- The tablestack function of the epidisplay package of R language makes a statistical summary table (descriptive statistics of groups, hypothesis test, etc.), does not set the by parameter to calculate
- R language GLM function logistic regression model, using epidisplay package logistic The display function obtains the summary statistical information of the model (initial and adjusted odds ratio and
- R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的均值和中位数、指定na.rm参数配置删除缺失值
- R语言glm函数逻辑回归模型、使用epiDisplay包logistic.display函数获取模型汇总统计信息(自变量初始和调整后的优势比及置信区间,回归系数的Wald检验的p值)、结果保存到csv
- R语言dplyr包intersect函数获取在两个dataframe中都存在的数据行、获取两个dataframe交叉的数据行
- Get the intersection union difference set of two dataframes
猜你喜欢

备战数学建模30-回归分析2

Halcon C# 设置窗体字体,自适应显示图片

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

MySQL master-slave replication and read-write separation

Mathematical modeling of war preparation 30 regression analysis 2
![[cloud native] codeless IVX editor programmable by](/img/10/7c56e46df69be6be522a477b00ec05.png)
[cloud native] codeless IVX editor programmable by "everyone"

TCP congestion control details | 1 summary

赠书 | 《认知控制》:我们的大脑如何完成任务?
杜老师说网站更新图解

View touch analysis
随机推荐
Informatics Olympiad all in one 1400: count the number of words (string matching)
[async/await] - the final solution of asynchronous programming
重磅白皮书发布,华为持续引领未来智慧园区建设新模式
R语言epiDisplay包的tableStack函数制作统计汇总表格(分组的描述性统计、假设检验等)、不设置by参数计算基础描述性统计信息、指定对于大多数样本负相关的变量进行反序
SAP 销售数据 实际发货数据导出 销量
Electron
Program analysis and Optimization - 8 register allocation
15 BS object Node name Node name String get nested node content
Kubernetes的pod调度
Halcon C# 设置窗体字体,自适应显示图片
房东拿租金去还房贷是天经地义的嘛
Is it safe to open an online stock account? Somebody give me an answer
fileinput. js php,fileinput
Redis集群消息
710. random numbers in the blacklist
【使用yarn运行报错】The engine “node“ is incompatible with this module.
R语言epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.X.axis参数指定X轴轴刻度数值标签字体的大小
JVM 输出 GC 日志导致 JVM 卡住,我 TM 人傻了
Electron
Two point answer, 01 score planning (mean / median conversion), DP