Unity教程之-unity3d实现文件断点下载续传

 

根据个人需要,研究了下unity线程下载方面的东西,网上这样的东西也很少我就在这和童鞋们说说,如果有童鞋有意见或建议可以一起交流下哦。

         首先先说说这个下载器的功能:
 
          1.      支持断点
          2.      支持下载临时文件自定义格式

   这个下载脚本就实现了这两个功能,其他的需要童鞋们自己去研究下哦,列如下载进度。
   使用这个脚本需要调用DownloadStart方法,传入参数六个参数:
 
          1.      下载链接
          2.      文件名(无后缀)
          3.      临时存放地址
          4.      正式存放地址
          5.      临时后缀
          6.      正式后缀
          注:临时地址和正式地址可以一样,临时后缀和正式后缀可以一样,但正式后缀是可运行的,未下载完成时运行会出错,所以童鞋们最好用一个临时后缀哦~
          其他的事情就不用管啦,文件会自己下载的,是不是很方便呢?

 

下面是我的代码,童鞋们可以取回去研究一下,不就不多说了


using UnityEngine;

using System.Collections;
using System.IO;
using System.Threading;
using System.Net;

public class wishDownload : MonoBehaviour
{
private string wish_DownLoadURL;
private string wish_Temporarypath;
private string wish_Installpath;
private string wish_FileName;
private string wish_Temporarysuffix;
private string wish_Filesuffix;
private Thread wish_Thread;

void Start()
{
System.Net.ServicePointManager.DefaultConnectionLimit = 512;
}

public void DownloadStart(string URL, string FileName, string Temporarypath, string Installpath, string Temporarysuffix, string Filesuffix)
{
wish_DownLoadURL = URL;
wish_FileName = FileName;
wish_Temporarypath = Temporarypath;
wish_Installpath = Installpath;
wish_Temporarysuffix = Temporarysuffix;
wish_Filesuffix = Filesuffix;

CheckFolder(wish_Temporarypath);
CheckFolder(wish_Installpath);

CheckFile(wish_FileName, URL);
}

void CheckFolder(string path)
{
if (Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
}
}

public void CheckFile(string fileName, string url)
{
if (File.Exists(wish_Installpath + "/" + fileName + wish_Filesuffix))
{
// 文件存在,不用下载
}
else
{
DownFile(fileName, url);
}
}

void DownFile(string fileName, string url)
{
DownThread DW = new DownThread(fileName + wish_Temporarysuffix, wish_Temporarypath + "/" + fileName + wish_Temporarysuffix, url, wish_Filesuffix, wish_Temporarysuffix, wish_Temporarypath, wish_Installpath);

wish_Thread = new Thread(new ThreadStart(DW.Down));

wish_Thread.Start();
}

class DownThread
{
string name;
string url;
string fileName;
long DownloadByte;
string Filesuffix;
string Temporarysuffix;
string Temporarypath;
string Installpath;

public DownThread(string fileName, string name, string url, string Filesuffix, string Temporarysuffix, string Temporarypath,string Installpath)
{
this.fileName = fileName;
this.name = name;
this.url = url;
this.Filesuffix = Filesuffix;
this.Temporarysuffix = Temporarysuffix;
this.Temporarypath = Temporarypath;
this.Installpath = Installpath;
}

public void Down()
{
DownloadByte = 0;
long lStartPos = 0;
FileStream fs;

if (File.Exists(name))
{
fs = File.OpenWrite(name);
lStartPos = fs.Length;
fs.Seek(lStartPos, SeekOrigin.Current);
}
else
{
fs = new FileStream(name, FileMode.Create);
lStartPos = 0;
}

try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
if (lStartPos > 0)
{
request.AddRange((int)lStartPos);
}
Stream ns = request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[8192];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, 8192);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);

nReadSize = ns.Read(nbytes, 0, 8192);
}

string changeName;
changeName = name.Substring(0, name.LastIndexOf("."));
changeName += Filesuffix;
changeName = changeName.Replace(Temporarypath, Installpath);

fs.Close();

ns.Close();

if (File.Exists(name))
{
Debug.Log(name);
Debug.Log(changeName);

try
{
File.Move(name, changeName);
}
catch (System.Exception e)
{
Debug.Log(e.ToString());
throw;
}
}
}
catch (System.Exception ex)
{
fs.Close();
}
}
}
}

好了,本篇unity3d文章关于,在unity3d中实现下载器的介绍到此结束,下篇我们再会!