当前位置:网站首页>Unity C # e-learning (10) -- unitywebrequest (2)
Unity C # e-learning (10) -- unitywebrequest (2)
2022-06-26 15:05:00 【Handsome_ shuai_】
Unity C# E-learning ( Ten )——UnityWebRequest( Two )
One .Unity Custom get data provided DownLoadHandler
Unity Help us define some related classes , The class used to parse the downloaded data , Use the corresponding class to handle the downloaded data , They will internally process the downloaded data into corresponding types , Easy to use
1.DownLoadHandlerBuffer( For simple data storage , Get the corresponding binary data )
private IEnumerator DownLoadBuffer()
{
UnityWebRequest req = new UnityWebRequest("http://192.168.1.103:8080/Http_Server/xxx.jpg",
UnityWebRequest.kHttpVerbGET);
DownloadHandlerBuffer downloadHandlerBuffer = new DownloadHandlerBuffer();
req.downloadHandler = downloadHandlerBuffer;
yield return req.SendWebRequest();
if (req.result == UnityWebRequest.Result.Success)
{
byte[] buffer = downloadHandlerBuffer.data;
Debug.Log(buffer.Length);
Debug.Log(" Download successful !");
}
else
{
Debug.Log(" Download failed :"+req.result);
}
}
2.DownLoadHandlerFile( For downloading files and saving them to disk , Less memory usage )
private IEnumerator DownLoadFile()
{
UnityWebRequest req = new UnityWebRequest("http://192.168.1.103:8080/Http_Server/xxx.jpg", UnityWebRequest.kHttpVerbGET);
req.downloadHandler = new DownloadHandlerFile(Application.persistentDataPath + "/WebDownLoad.jpg");
yield return req.SendWebRequest();
Debug.Log(req.result);
}
3.DownLoadHandlerTexture( For downloading pictures )
private IEnumerator DownLoadTexture()
{
UnityWebRequest req = new UnityWebRequest("http://192.168.1.103:8080/Http_Server/xxx.jpg", UnityWebRequest.kHttpVerbGET);
DownloadHandlerTexture downloadHandlerTexture = new DownloadHandlerTexture();
req.downloadHandler = downloadHandlerTexture;
yield return req.SendWebRequest();
if (req.result == UnityWebRequest.Result.Success)
{
Debug.Log(downloadHandlerTexture.texture);
Debug.Log(" Download successful !");
}
else
{
Debug.Log(" Download failed :"+req.result);
}
}
4.DownLoadHandlerAssetBundle( For extraction AssetBundle)
private IEnumerator DownLoadAssetBundle()
{
UnityWebRequest req = new UnityWebRequest("http://192.168.1.103:8080/Http_Server/photo.ywj", UnityWebRequest.kHttpVerbGET);
DownloadHandlerAssetBundle downloadHandlerAssetBundle = new DownloadHandlerAssetBundle(req.url,0);
req.downloadHandler = downloadHandlerAssetBundle;
yield return req.SendWebRequest();
if (req.result == UnityWebRequest.Result.Success)
{
Debug.Log(downloadHandlerAssetBundle.assetBundle.name);
Debug.Log(" Download successful !");
}
else
{
Debug.Log(" Download failed :"+req.result);
}
}
5.DownLoadHandlerAudioClip( For downloading audio files )
private IEnumerator DownLoadAudioClip()
{
UnityWebRequest req = new UnityWebRequest("http://192.168.1.103:8080/Http_Server/music.mp3", UnityWebRequest.kHttpVerbGET);
DownloadHandlerAudioClip downloadHandlerAudioClip = new DownloadHandlerAudioClip(req.url,AudioType.MPEG);
req.downloadHandler = downloadHandlerAudioClip;
yield return req.SendWebRequest();
if (req.result == UnityWebRequest.Result.Success)
{
Debug.Log(downloadHandlerAudioClip.audioClip);
Debug.Log(" Download successful !");
}
else
{
Debug.Log(" Download failed :"+req.result);
}
}
Two . Customize DownLoadHandler To process data
- When we need to expand the data , Can be inherited from DownloadHandlerScript Class to extend it
public class CustomDownLoadFileHandler : DownloadHandlerScript
{
private string _savePath;
private byte[] _cacheBytes;
private int _index;
public CustomDownLoadFileHandler() : base()
{
}
public CustomDownLoadFileHandler(byte[] bytes) : base(bytes)
{
}
public CustomDownLoadFileHandler(string path) : base()
{
this._savePath = path;
}
protected override byte[] GetData()
{
return _cacheBytes;
}
// After receiving data from the network , Methods that will be called at each frame
protected override bool ReceiveData(byte[] buffer, int dataLength)
{
buffer.CopyTo(_cacheBytes,_index);
_index += dataLength;
return true;
}
// Received... From server contentLength Header will call
protected override void ReceiveContentLengthHeader(ulong contentLength)
{
_cacheBytes = new byte[contentLength];
}
// The message has been received , Methods that will be called automatically
protected override void CompleteContent()
{
File.WriteAllBytes(_savePath,_cacheBytes);
}
}
To test
private IEnumerator CustomDownLoadFile()
{
UnityWebRequest unityWebRequest = new UnityWebRequest("http://192.168.1.103:8080/Http_Server/xxx.jpg",
UnityWebRequest.kHttpVerbGET);
unityWebRequest.downloadHandler = new CustomDownLoadFileHandler(Application.persistentDataPath+"/zzsCustom.jpg");
yield return unityWebRequest.SendWebRequest();
Debug.Log(unityWebRequest.result);
}
3、 ... and . Customize DownLoadHandler Exercise handling
public class DownLoadHandlerMsg : DownloadHandlerScript
{
private byte[] _cacheBuffer;
private int _index;
private MsgBase _msgBase;
public DownLoadHandlerMsg()
{
}
public T GetMsg<T>() where T : MsgBase
{
return _msgBase as T;
}
protected override byte[] GetData()
{
return _cacheBuffer;
}
protected override void ReceiveContentLengthHeader(ulong contentLength)
{
_cacheBuffer = new byte[contentLength];
}
protected override bool ReceiveData(byte[] buffer, int dataLength)
{
buffer.CopyTo(_cacheBuffer,_index);
_index += dataLength;
return true;
}
protected override void CompleteContent()
{
int index = 0;
int msgId = BitConverter.ToInt32(_cacheBuffer,index);
index += 4;
int msgLength = BitConverter.ToInt32(_cacheBuffer, index);
index += 4;
switch (msgId)
{
case 1001:
_msgBase = new PlayerMsg();
_msgBase.Reading(_cacheBuffer, index);
break;
}
Debug.Log(_msgBase == null ? " news Id error !" : " Message parsing succeeded !");
}
}
Test code
private IEnumerator Start()
{
UnityWebRequest unityWebRequest = new UnityWebRequest("", UnityWebRequest.kHttpVerbGET);
unityWebRequest.downloadHandler = new DownLoadHandlerMsg();
yield return unityWebRequest.SendWebRequest();
Debug.Log(unityWebRequest.result);
}
边栏推荐
- RestCloud ETL解决shell脚本参数化
- Smoothing data using convolution
- 710. random numbers in the blacklist
- TCP congestion control details | 1 summary
- Unity UnityWebRequest 下载封装
- R语言dplyr包bind_rows函数把两个dataframe数据的行纵向(竖直)合并起来、最终行数为原来两个dataframe行数的加和(Combine Data Frames)
- TCP拥塞控制详解 | 1. 概述
- R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、使用aggregate.data.frame函数计算分组汇总统计信息
- Kubernetes的pod调度
- Numpy basic use
猜你喜欢

北京银行x华为:网络智能运维夯实数字化转型服务底座

设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球

ETL过程中数据精度不准确问题

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

Notes on writing questions in C language -- table tennis competition

Smoothing data using convolution

The engine "node" is inconsistent with this module

Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type

Halcon C # sets the form font and adaptively displays pictures

RestCloud ETL与Kettle对比分析
随机推荐
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
cluster addslots建立集群
IP certificate application process of sectigo
Unity C# 网络学习(九)——WWWFrom
clustermeet
TCP congestion control details | 1 summary
程序分析与优化 - 8 寄存器分配
kubernetes的Controller之deployment
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、使用summary函数查看简单模型的汇总统计信息
Redis cluster messages
一键分析硬件/IO/全国网络性能脚本(强推)
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
Minister of investment of Indonesia: Hon Hai is considering establishing electric bus system and urban Internet of things in its new capital
Restcloud ETL extraction de données de table de base de données dynamique
Kubernetes的pod调度
网上股票开户安不安全?谁给回答一下
clustermeet
印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网
View touch analysis
详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型