Unity帮我们定义好了一些相关的类,用于解析下载下来的数据的类,使用对应的类处理下载数据,他们就会在内部将下载的数据处理为对应的类型,方便使用
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("下载成功!");
}
else
{
Debug.Log("下载失败:"+req.result);
}
}
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);
}
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("下载成功!");
}
else
{
Debug.Log("下载失败:"+req.result);
}
}
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("下载成功!");
}
else
{
Debug.Log("下载失败:"+req.result);
}
}
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("下载成功!");
}
else
{
Debug.Log("下载失败:"+req.result);
}
}
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;
}
//从网络收到数据后,每帧会调用的方法
protected override bool ReceiveData(byte[] buffer, int dataLength)
{
buffer.CopyTo(_cacheBytes,_index);
_index += dataLength;
return true;
}
//从服务器收到contentLength标头时会调用
protected override void ReceiveContentLengthHeader(ulong contentLength)
{
_cacheBytes = new byte[contentLength];
}
//消息收完了,会自动调用的方法
protected override void CompleteContent()
{
File.WriteAllBytes(_savePath,_cacheBytes);
}
}
进行测试
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);
}
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 ? "消息Id错误!" : "消息解析成功!");
}
}
测试代码
private IEnumerator Start()
{
UnityWebRequest unityWebRequest = new UnityWebRequest("", UnityWebRequest.kHttpVerbGET);
unityWebRequest.downloadHandler = new DownLoadHandlerMsg();
yield return unityWebRequest.SendWebRequest();
Debug.Log(unityWebRequest.result);
}