unity 线程下载图片

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 02:11   24   0

1、线程下载

代码

[csharp]view plaincopy
  1. usingSystem.IO;
  2. usingSystem.Net;
  3. usingUnityEngine;
  4. usingSystem.Threading;
  5. usingSystem.Collections;
  6. publicclassThreadTest:MonoBehaviour{
  7. privatestring[]_urls=newstring[10];
  8. privatestring[]_localPath=newstring[10];
  9. //Usethisforinitialization
  10. voidStart()
  11. {
  12. for(inti=0;i<_urls.Length;i++)
  13. {
  14. //所有图片的下载地址
  15. _urls[i]="http://192.168.1.41:8080/Test/picture/"+(i+1).ToString()+".jpg";
  16. //所有图片的保存路径
  17. _localPath[i]=Application.dataPath+"/Resources/"+(i+1).ToString()+".jpg";
  18. MyThreadmt=newMyThread(_urls[i],_localPath[i]);
  19. Threadthread=newThread(newThreadStart(mt.DownLoadImage));
  20. thread.Start();
  21. }
  22. }
  23. //Updateiscalledonceperframe
  24. voidUpdate()
  25. {
  26. }
  27. voidOnGUI()
  28. {
  29. }
  30. }
  31. publicclassMyThread
  32. {
  33. publicstring_url;
  34. publicstring_filePath;
  35. publicMyThread(stringurl,stringfilePath)
  36. {
  37. _url=url;
  38. _filePath=filePath;
  39. }
  40. publicvoidDownLoadImage()
  41. {
  42. WebClientweb=newWebClient();
  43. web.DownloadFile(_url,_filePath);
  44. }
  45. }


2、批量下载




代码如下

[csharp]view plaincopy
  1. usingSystem.IO;
  2. usingUnityEngine;
  3. usingSystem.Net;
  4. usingSystem.Collections;
  5. publicclassTest:MonoBehaviour{
  6. privatestring[]_urls=newstring[10];
  7. privatestring[]_localPath=newstring[10];
  8. //Usethisforinitialization
  9. voidStart()
  10. {
  11. for(inti=0;i<_urls.Length;i++)
  12. {
  13. //所有图片的下载地址
  14. _urls[i]="http://192.168.1.41:8080/Test/picture/"+(i+1).ToString()+".jpg";
  15. //所有图片的保存路径
  16. _localPath[i]=Application.dataPath+"/Resources/"+(i+1).ToString()+".jpg";
  17. }
  18. }
  19. //Updateiscalledonceperframe
  20. voidUpdate()
  21. {
  22. }
  23. voidOnGUI()
  24. {
  25. if(GUI.Button(newRect(0,0,100,30),"下载所有图片"))
  26. {
  27. DownLoad();
  28. }
  29. //判断文件是否已下载
  30. for(inti=0;i<_urls.Length;i++)
  31. {
  32. if(File.Exists(_localPath[i]))
  33. {
  34. GUI.Button(newRect(0,30*i+30,50,30),(i+1).ToString());
  35. }
  36. }
  37. }
  38. //下载所有图片
  39. privatevoidDownLoad()
  40. {
  41. for(inti=0;i<_urls.Length;i++)
  42. {
  43. DownLoadAllImages(_urls[i],_localPath[i]);
  44. }
  45. }
  46. voidDownLoadAllImages(stringurl,stringlocalPath)
  47. {
  48. WebClientweb=newWebClient();
  49. web.DownloadFile(url,localPath);
  50. //以下代码下载完成后执行
  51. }
  52. }


  53. 原文链接:

    http://blog.csdn.net/awnuxcvbn/article/details/9246633版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP