unity3d 关于断点下载和整个下载(用于更新)

   

unity3d 关于断点下载和整个下载(用于更新){转}

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4. using System.Net;  
  5. using Math= System.Math;  
  6. using Uri = System.Uri;  
  7. using Convert= System.Convert;  
  8. public class DownloadTest : MonoBehaviour  
  9. {  
  10.   
  11.     public UILabel label1;  
  12.     public UILabel label2;  
  13.     // Use this for initialization  
  14.     void Start ()  
  15.     {  
  16.       
  17.     }  
  18.       
  19.     // Update is called once per frame  
  20.     void Update ()  
  21.     {  
  22.       
  23.     }  
  24.   
  25.   
  26.     void OnGUI ()  
  27.     {  
  28.         if (GUI.Button (new Rect (0, 0, 100, 20), "Download")) {  
  29.             StartCoroutine (downfile ("http://localhost:8888/test.zip", Application.streamingAssetsPath + "/test.zip", label1));  
  30.         }  
  31.         if (GUI.Button (new Rect (0, 20, 100, 20), "StopDownload")) {  
  32.             StopAllCoroutines ();  
  33.         }  
  34.         if (GUI.Button (new Rect (0, 40, 100, 20), "ResumDownload")) {  
  35.             StartCoroutine (FPointDown ("http://localhost:8888/test.zip", Application.streamingAssetsPath + "/test.zip", label2));  
  36.         }  
  37.     }  
  38.   
  39.     string t = "";  
  40.     //整体下载  
  41.     IEnumerator downfile (string url, string LocalPath, UILabel DesLable)  
  42.     {  
  43.         Uri u = new Uri (url);  
  44.         HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create (u);  
  45.         mRequest.Method = "GET";  
  46.         mRequest.ContentType = "application/x-www-form-urlencoded";  
  47.           
  48.         HttpWebResponse wr = (HttpWebResponse)mRequest.GetResponse ();  
  49.           
  50.         Stream sIn = wr.GetResponseStream ();  
  51.         FileStream fs = new FileStream (LocalPath, FileMode.Create, FileAccess.Write);  
  52.           
  53.         long length = wr.ContentLength;  
  54.         long i = 0;  
  55.         decimal j = 0;  
  56.           
  57.         while (i<length) {  
  58.             byte[] buffer = new byte[1024];  
  59.             i += sIn.Read (buffer, 0, buffer.Length);  
  60.             fs.Write (buffer, 0, buffer.Length);  
  61.               
  62.             if ((i % 1024) == 0) {  
  63.                 j = Math.Round (Convert.ToDecimal ((Convert.ToDouble (i) / Convert.ToDouble (length)) * 100), 4);  
  64.                 t = "当前下载文件大小:" + length.ToString () + "字节当前下载大小:" + i + "字节下载进度" + j.ToString () + "%";  
  65.                 DesLable.text = t.ToString ();  
  66.             } else {  
  67.                 t = "当前下载文件大小:" + length.ToString () + "字节当前下载大小:" + i + "字节";  
  68.                 DesLable.text = t.ToString ();  
  69.             }  
  70.               
  71.             yield return false;  
  72.               
  73.               
  74.         }  
  75.           
  76.         sIn.Close ();  
  77.         wr.Close ();  
  78.         fs.Close ();  
  79.           
  80.     }  
  81.     string downloadString = "已经下载";  
  82.     //断点下载  
  83.     IEnumerator FPointDown (string uri, string saveFile, UILabel DesLable)  
  84.     {  
  85.         //打开网络连接  
  86.         HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create (uri);  
  87.         HttpWebRequest requestGetCount = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create (uri);  
  88.         long countLength = requestGetCount.GetResponse ().ContentLength;  
  89.           
  90.         //打开上次下载的文件或新建文件  
  91.         long lStartPos = 0;  
  92.         System.IO.FileStream fs;  
  93.         if (System.IO.File.Exists (saveFile)) {  
  94.             fs = System.IO.File.OpenWrite (saveFile);  
  95.             lStartPos = fs.Length;  
  96.             if (countLength - lStartPos <= 0) {  
  97.                 fs.Close ();  
  98.                 t = "已经";  
  99.                 DesLable.text = t.ToString ();  
  100.                 yield break;  
  101.             }  
  102.             fs.Seek (lStartPos, System.IO.SeekOrigin.Current);//移动文件流中的当前指针  
  103.         } else {  
  104.             fs = new FileStream (saveFile, FileMode.Create);  
  105.         }  
  106.           
  107.           
  108.         if (lStartPos > 0) {  
  109.             request.AddRange ((int)lStartPos);//设置Range值  
  110.             print (lStartPos);  
  111.         }  
  112.           
  113.         //向服务器请求,获得服务器回应数据流  
  114.         Stream ns = request.GetResponse ().GetResponseStream ();  
  115.         int len = 1024 * 8;  
  116.           
  117.         byte[] nbytes = new byte[len];  
  118.         int nReadSize = 0;  
  119.         nReadSize = ns.Read (nbytes, 0, len);  
  120.         while (nReadSize>0) {  
  121.             fs.Write (nbytes, 0, nReadSize);  
  122.             nReadSize = ns.Read (nbytes, 0, len);  
  123.             t = downloadString + ":" + fs.Length / 1024 + "kb/" + countLength / 1024 + "kb" + "----" + ((double)fs.Length / countLength).ToString () + "%";  
  124.               
  125.             DesLable.text=t;  
  126.             yield return false;  
  127.         }  
  128.         ns.Close ();  
  129.         fs.Close ();  
  130.         //这里放更新安装代码,或者可以测试这个下载的包有没有出错,验证sha和md5  
  131.   
  132.     }   
  133. }  

posted on 2016-11-22 18:56  lifebuilder  阅读(149)  评论(0)    收藏  举报

导航