unityブレークポイントダウンロード

11529 ワード

using UnityEngine;

using System.Collections;

using System;

using System.Net;

using System.IO;



public class Load : MonoBehaviour {



    AsyncOperation l;

    void Start () {

       // Application.backgroundLoadingPriority = ThreadPriority.Low;

       // l =  Application.LoadLevelAsync("game");

        StartCoroutine(FPointDown("http://127.0.0.1:8888/index.rar", "d:/a.rar"));

        StartCoroutine(FPointDown(@"http://dl_dir.qq.com/qqfile/qq/QQ2012/QQ2012Beta3.exe", "d:/qq.rar"));

        

    }

    



    void OnGUI () {

        GUILayout.Button(t);

    }

    void OnApplicationQuit()

    {

        print("stop");

        StopCoroutine("FPointDown");

    }

    string t = "";

    IEnumerator  downfile(string url, string LocalPath)

    {



            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= " :" + length.ToString() + "     :" + i + "   " + j.ToString() + "%";



                }

                else

                {

                   t = " :" + length.ToString() + "     :" + i + " ";

                }

                yield return false;





            }



            sIn.Close();

            wr.Close();

            fs.Close();

     

    }



    // 

    IEnumerator FPointDown(string uri,string saveFile)

    {

        //  

        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;

     

        //  

        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 = " ";

                yield break;

            }

            fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //  

        }

        else

        {

            fs = new System.IO.FileStream(saveFile, System.IO.FileMode.Create);

        }

      



        if (lStartPos > 0)

        {

            request.AddRange((int)lStartPos); // Range 

            print(lStartPos);

        }



        //

        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 = " :" + fs.Length / 1024 + "kb /" + countLength / 1024 + "kb";

            yield return false;

        }

        ns.Close();

        fs.Close();

    }

}