1、线程下载

代码
-
usingSystem.IO;
-
usingSystem.Net;
-
usingUnityEngine;
-
usingSystem.Threading;
-
usingSystem.Collections;
-
-
publicclassThreadTest:MonoBehaviour{
-
-
privatestring[]_urls=newstring[10];
-
privatestring[]_localPath=newstring[10];
-
-
-
-
-
voidStart()
-
{
-
-
for(inti=0;i<_urls.Length;i++)
-
{
-
-
_urls[i]="http://192.168.1.41:8080/Test/picture/"+(i+1).ToString()+".jpg";
-
-
-
_localPath[i]=Application.dataPath+"/Resources/"+(i+1).ToString()+".jpg";
-
-
-
MyThreadmt=newMyThread(_urls[i],_localPath[i]);
-
Threadthread=newThread(newThreadStart(mt.DownLoadImage));
-
thread.Start();
-
-
}
-
-
}
-
-
-
voidUpdate()
-
{
-
-
}
-
-
voidOnGUI()
-
{
-
-
}
-
}
-
-
publicclassMyThread
-
{
-
publicstring_url;
-
publicstring_filePath;
-
-
publicMyThread(stringurl,stringfilePath)
-
{
-
_url=url;
-
_filePath=filePath;
-
}
-
-
publicvoidDownLoadImage()
-
{
-
-
WebClientweb=newWebClient();
-
web.DownloadFile(_url,_filePath);
-
}
-
}
2、批量下载


代码如下
-
usingSystem.IO;
-
usingUnityEngine;
-
usingSystem.Net;
-
usingSystem.Collections;
-
-
publicclassTest:MonoBehaviour{
-
-
privatestring[]_urls=newstring[10];
-
privatestring[]_localPath=newstring[10];
-
-
-
voidStart()
-
{
-
for(inti=0;i<_urls.Length;i++)
-
{
-
-
_urls[i]="http://192.168.1.41:8080/Test/picture/"+(i+1).ToString()+".jpg";
-
-
_localPath[i]=Application.dataPath+"/Resources/"+(i+1).ToString()+".jpg";
-
-
}
-
}
-
-
-
voidUpdate()
-
{
-
-
}
-
-
voidOnGUI()
-
{
-
if(GUI.Button(newRect(0,0,100,30),"下载所有图片"))
-
{
-
DownLoad();
-
}
-
-
for(inti=0;i<_urls.Length;i++)
-
{
-
if(File.Exists(_localPath[i]))
-
{
-
GUI.Button(newRect(0,30*i+30,50,30),(i+1).ToString());
-
}
-
}
-
-
-
}
-
-
-
privatevoidDownLoad()
-
{
-
for(inti=0;i<_urls.Length;i++)
-
{
-
DownLoadAllImages(_urls[i],_localPath[i]);
-
}
-
}
-
-
-
voidDownLoadAllImages(stringurl,stringlocalPath)
-
{
-
WebClientweb=newWebClient();
-
web.DownloadFile(url,localPath);
-
-
}
-
}
-
-
-
原文链接:
http://blog.csdn.net/awnuxcvbn/article/details/9246633版权声明:本文为博主原创文章,未经博主允许不得转载。
|
|