当前位置:网站首页>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);
}
边栏推荐
- Document 1
- Numpy基本使用
- Mark: unity3d cannot select resources in the inspector, that is, project locking
- How to mount cloud disks in ECS
- 设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
- 【 Native cloud】 Éditeur ivx Programmable par tout le monde
- Is it safe to open an account by digging money? Is there any risk?
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、使用summary函数查看简单模型的汇总统计信息
- Deploy the flask environment using the pagoda panel
- Notes on writing questions in C language -- table tennis competition
猜你喜欢

使用 Abp.Zero 搭建第三方登录模块(二):服务端开发

Where do people get their top energy?

印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网

详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型

About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)

重磅白皮书发布,华为持续引领未来智慧园区建设新模式

TCP congestion control details | 1 summary

teamviewer显示设备数量上限解决方法

Document 1

qt下多个子控件信号槽绑定方法
随机推荐
R language dplyr package bind_ The rows function merges the rows of the two dataframes vertically. The final number of rows is the sum of the rows of the original two dataframes (combine data frames)
获取两个dataframe的交并差集
Unity C# 网络学习(十)——UnityWebRequest(二)
Unity C# 网络学习(十)——UnityWebRequest(一)
Login authentication service
Unity uses skybox panoramic shader to make panorama preview. There is a gap. Solution
Use of subqueries
Optimizing for vectorization
[solo π] ADB connects multiple mobile phones
Where do people get their top energy?
[async/await] - the final solution of asynchronous programming
【雲原生】 ”人人皆可“ 編程的無代碼 iVX 編輯器
Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type
redis集群的重新分片与ASK命令
qt下多个子控件信号槽绑定方法
【使用yarn运行报错】The engine “node“ is incompatible with this module.
设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
Redis-集群
Go变量的声明与赋值
使用RestCloud ETL Shell组件实现定时调度DataX离线任务