Created
March 17, 2017 09:08
-
-
Save sunny352/0c33446cc9cd9b9c4203e8798363f002 to your computer and use it in GitHub Desktop.
Unity内Http下载代码,要求Unity5.4以上(UnityEngine.Networking),5.4以下支持UnityWebRequest的也可以修改using后使用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using UnityEngine.Networking; | |
public class DownloadFileHandler : DownloadHandlerScript | |
{ | |
private FileStream mFileStream; | |
private HttpUtil.DownloadCallback mDownloadCallback; | |
private HttpUtil.FinishedCallback mFinishedCallback; | |
public DownloadFileHandler(FileStream fileStream, HttpUtil.DownloadCallback downloadCallback, HttpUtil.FinishedCallback finishedCallback) | |
{ | |
mFileStream = fileStream; | |
mDownloadCallback = downloadCallback; | |
mFinishedCallback = finishedCallback; | |
} | |
private int totalLength; | |
private int downloadedLength; | |
protected override void CompleteContent() | |
{ | |
if (null != mFinishedCallback) | |
{ | |
mFinishedCallback(totalLength); | |
} | |
} | |
protected override float GetProgress() | |
{ | |
return (float)downloadedLength / (float)totalLength; | |
} | |
protected override void ReceiveContentLength(int contentLength) | |
{ | |
totalLength = contentLength; | |
} | |
protected override bool ReceiveData(byte[] data, int dataLength) | |
{ | |
downloadedLength += dataLength; | |
mFileStream.Write(data, 0, dataLength); | |
if (null != mDownloadCallback) | |
{ | |
mDownloadCallback(downloadedLength, totalLength, GetProgress()); | |
} | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.IO; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
public static class HttpUtil | |
{ | |
public enum ENFileExist | |
{ | |
enReplace, //替换 | |
enCancel, //取消下载 | |
} | |
public delegate void DownloadCallback(int current, int total, float progress); | |
public delegate void FinishedCallback(int downloaded); | |
public static IEnumerator Download(string url, string fileName, ENFileExist existOpt, DownloadCallback downloadCallback, FinishedCallback finishedCallback) | |
{ | |
string finalFilePath = Application.persistentDataPath + "/" + fileName; | |
FileInfo finalFileInfo = new FileInfo(finalFilePath); | |
if (finalFileInfo.Exists) | |
{ | |
switch (existOpt) | |
{ | |
case ENFileExist.enReplace: | |
break; | |
case ENFileExist.enCancel: | |
yield break; | |
default: | |
break; | |
} | |
} | |
string tempFilePath = Application.temporaryCachePath + "/" + fileName + ".tmp"; | |
FileInfo tempFileInfo = new FileInfo(tempFilePath); | |
using (FileStream fileStream = File.Open(tempFilePath, tempFileInfo.Exists ? FileMode.Append : FileMode.CreateNew)) | |
{ | |
using (UnityWebRequest request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET, new DownloadFileHandler(fileStream, downloadCallback, finishedCallback), null)) | |
{ | |
if (tempFileInfo.Exists) | |
{ | |
request.SetRequestHeader("RANGE", string.Format("bytes={0}-", tempFileInfo.Length)); | |
} | |
yield return request.Send(); | |
} | |
} | |
if (finalFileInfo.Exists) | |
{ | |
File.Delete(finalFilePath); | |
} | |
File.Move(tempFilePath, finalFilePath); | |
File.Delete(tempFilePath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment