Transferred from: http://blog.csdn.net/yutyliu/article/details/24346459
String t = "";
Overall download
IEnumerator downfile (string url, string Localpath,uilabel deslable)
{
Uri u = new uri (URL);
HttpWebRequest mrequest = (HttpWebRequest) webrequest.create (U);
Mrequest.method = "GET";
Mrequest.contenttype = "application/x-www-form-urlencoded";
HttpWebResponse WR = (HttpWebResponse) mrequest.getresponse ();
Stream sIn = wr. GetResponseStream ();
FileStream fs = new FileStream (LocalPath, FileMode.Create, FileAccess.Write);
Long length = wr. ContentLength;
Long i = 0;
Decimal j = 0;
while (I < length)
{
byte[] buffer = new byte[1024];
i + = sin.read (buffer, 0, buffer. Length);
Fs. Write (buffer, 0, buffer. Length);
if ((i% 1024) = = 0)
{
j = Math.Round (Convert.todecimal ((convert.todouble (i)/convert.todouble (length)) * 100), 4);
t= "Current download File size:" + length. ToString () + "bytes Current Download size:" + i + "bytes Download Progress" + j.tostring () + "%";
Deslable.text=t.tostring ();
}
Else
{
t = "Current download file size:" + length. ToString () + "byte current download size:" + i + "byte";
Deslable.text=t.tostring ();
}
Yield return false;
}
Sin.close ();
Url Close ();
Fs. Close ();
}
String downloadstring= "already downloaded";
Breakpoint Download
IEnumerator Fpointdown (String uri,string savefile,uilabel deslable,uisprite loadsprite)
{
Open Network Connection
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create (URI);
System.Net.HttpWebRequest Requestgetcount = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create (URI);
Long countlength = Requestgetcount.getresponse (). ContentLength;
Open the last downloaded file or create a new file
Long Lstartpos = 0;
System.IO.FileStream FS;
if (System.IO.File.Exists (saveFile))
{
FS = System.IO.File.OpenWrite (SaveFile);
Lstartpos = fs. Length;
if (countlength-lstartpos <= 0)
{
Fs. Close ();
t = "already";
Deslable.text=t.tostring ();
Yield break;
}
Fs. Seek (Lstartpos, System.IO.SeekOrigin.Current); Move the current pointer in a file stream
}
Else
{
FS = new System.IO.FileStream (SaveFile, System.IO.FileMode.Create);
}
if (Lstartpos > 0)
{
Request. AddRange ((int) lstartpos); Set Range value
Print (Lstartpos);
}
Request to server for server response data flow
System.IO.Stream ns = Request. GetResponse (). GetResponseStream ();
int len = 1024 * 8;
byte[] nbytes = new Byte[len];
int nreadsize = 0;
Nreadsize = ns. Read (nbytes, 0, Len);
while (Nreadsize > 0)
{
Fs. Write (nbytes, 0, nreadsize);
Nreadsize = ns. Read (nbytes, 0, Len);
t = downloadstring+ ":" + FS. length/1024 + "kb/" + countlength/1024 + "KB" + "----" + ((double) fs. Length/countlength). ToString () + "%";
Yield return false;
}
Ns. Close ();
Fs. Close ();
Put the update installation code here, or you can test the downloaded package for errors, verify that SHA and MD5
Unity3d about breakpoints Download and entire download (for updates)