简单得 使用线程池,和WebClient 结合下载资源到本地
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
public class TheadEvent
{
public string url { set; get; }
public string savePath { set; get; }
public Stopwatch sw = new Stopwatch();
}
public class TestDownLoad : MonoBehaviour {
public Action<byte[]> actionImage;
public RawImage img;
private Stopwatch sw = new Stopwatch();
private readonly object sysob = new object();
int flag;
// Use this for initialization
void Start () {
ThreadPool.SetMaxThreads(2, 2);
ThreadPool.SetMinThreads(1, 1);
TheadEvent t1 = new TheadEvent();
t1.url= "http://127.0.0.1/0.mp4";
t1.savePath = "E:\\testFile\\0.mp4";
TheadEvent t2 = new TheadEvent();
t2.url = "http://127.0.0.1/1.mp4";
t2.savePath = "E:\\testFile\\1.mp4";
TheadEvent t3 = new TheadEvent();
t3.url = "http://127.0.0.1/2.mp4";
t3.savePath = "E:\\testFile\\2.mp4";
TheadEvent t4 = new TheadEvent();
t4.url = "http://127.0.0.1/购票信息.txt";
t4.savePath = "E:\\testFile\\购票信息.txt";
TheadEvent t5 = new TheadEvent();
t5.url = "http://127.0.0.1/weixin_1220.apk";
t5.savePath = "E:\\testFile\\weixin_1220.apk";
TheadEvent t6 = new TheadEvent();
t6.url = "http://127.0.0.1/5.png";
t6.savePath = "E:\\testFile\\5.png";
Add(t1);
Add(t2);
Add(t3);
Add(t4);
Add(t5);
Add(t6);
//for (int i=0;i<60;i++)
// {
// ThreadPool.QueueUserWorkItem(new WaitCallback(Computer) , i.ToString());
// }
UnityEngine.Debug.Log("主线程结束");
}
void Computer(object ob)
{
lock(sysob)
{
int b = 0;
UnityEngine.Debug.Log("_线程:" + ob.ToString() + "_执行时间:" + DateTime.UtcNow);
for (int i = 0; i < 10000; i++)
{
b += i;
}
UnityEngine.Debug.Log("_线程:" + ob.ToString() + "_计算结果:" + b);
}
}
void Add(TheadEvent threadEvent)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(OnDownLoad), threadEvent);
}
void OnDownLoad(object ob)
{
lock(sysob)
{
TheadEvent threadEvent = ob as TheadEvent;
using (WebClient webClient = new WebClient())
{
HttpWebRequest request = new HttpWebRequest(new Uri(threadEvent.url));
threadEvent.sw.Start();
webClient.DownloadProgressChanged += DownloadProgressChanged;
// webClient.DownloadDataAsync(new Uri(threadEvent.url), threadEvent);
// webClient.DownloadDataCompleted += WebClient_DownloadDataCompleted;
webClient.DownloadFileAsync(new Uri(threadEvent.url), threadEvent.savePath,threadEvent);
// UnityEngine.Debug.Log("_线程:" + ob.ToString());
}
Thread.Sleep(200);
}
}
private void WebClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
TheadEvent t = e.UserState as TheadEvent;
byte[] bs= e.Result;
UnityEngine.Debug.Log(t.url+"_内容:"+Encoding.UTF8.GetString(bs) );
}
private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
TheadEvent t = e.UserState as TheadEvent;
string value = (e.BytesReceived / 1024d / t.sw.Elapsed.TotalSeconds).ToString();
UnityEngine.Debug.Log(t.url+"下载速度:"+value + "/kb");
if (e.ProgressPercentage==100&&e.BytesReceived==e.TotalBytesToReceive)
{
t.sw.Reset();
UnityEngine.Debug.LogError("downLoad Over:"+ t.url);
}
}
}