unity 线程下载图片

1、线程下载

代码

[csharp]  view plain copy
  1. using System.IO;  
  2. using System.Net;  
  3. using UnityEngine;  
  4. using System.Threading;  
  5. using System.Collections;  
  6.   
  7. public class ThreadTest : MonoBehaviour {  
  8.   
  9.     private string[] _urls = new string[10];  
  10.     private string[] _localPath = new string[10];  
  11.   
  12.      
  13.   
  14.     // Use this for initialization  
  15.     void Start ()   
  16.     {  
  17.         
  18.         for (int i = 0; i < _urls.Length; i++)  
  19.         {  
  20.             //所有图片的下载地址  
  21.             _urls[i] = "http://192.168.1.41:8080/Test/picture/" + (i + 1).ToString() + ".jpg";  
  22.               
  23.             //所有图片的保存路径  
  24.             _localPath[i] = Application.dataPath + "/Resources/" + (i + 1).ToString() + ".jpg";  
  25.              
  26.   
  27.             MyThread mt = new MyThread(_urls[i], _localPath[i]);  
  28.             Thread thread = new Thread(new ThreadStart(mt.DownLoadImage));  
  29.             thread.Start();  
  30.               
  31.         }  
  32.   
  33.     }  
  34.       
  35.     // Update is called once per frame  
  36.     void Update ()   
  37.     {  
  38.       
  39.     }  
  40.   
  41.     void OnGUI()  
  42.     {  
  43.   
  44.     }  
  45. }  
  46.   
  47. public class MyThread  
  48. {  
  49.     public string _url;  
  50.     public string _filePath;  
  51.   
  52.     public MyThread(string url, string filePath)  
  53.     {  
  54.         _url = url;  
  55.         _filePath = filePath;  
  56.     }  
  57.   
  58.     public void DownLoadImage()  
  59.     {  
  60.           
  61.         WebClient web = new WebClient();  
  62.         web.DownloadFile(_url, _filePath);  
  63.     }  
  64. }  


2、批量下载




代码如下

[csharp]  view plain copy
  1. using System.IO;  
  2. using UnityEngine;  
  3. using System.Net;  
  4. using System.Collections;  
  5.   
  6. public class Test : MonoBehaviour {  
  7.   
  8.     private string[] _urls=new string[10];  
  9.     private string [] _localPath = new string[10];  
  10.   
  11.     // Use this for initialization  
  12.     void Start ()   
  13.     {  
  14.         for (int i = 0; i < _urls.Length; i++)  
  15.         {  
  16.             //所有图片的下载地址  
  17.             _urls[i] = "http://192.168.1.41:8080/Test/picture/" + (i + 1).ToString() + ".jpg";  
  18.             //所有图片的保存路径  
  19.             _localPath[i] = Application.dataPath + "/Resources/" + (i + 1).ToString() + ".jpg";  
  20.               
  21.         }  
  22.     }  
  23.   
  24.     // Update is called once per frame  
  25.     void Update()   
  26.     {  
  27.       
  28.     }  
  29.   
  30.     void OnGUI()  
  31.     {  
  32.         if (GUI.Button(new Rect(0, 0, 100, 30), "下载所有图片"))  
  33.         {  
  34.             DownLoad();  
  35.         }  
  36.         //判断文件是否已下载  
  37.         for (int i = 0; i < _urls.Length; i++)  
  38.         {  
  39.             if (File.Exists(_localPath[i]))  
  40.             {  
  41.                 GUI.Button(new Rect(0, 30 * i+30, 50, 30), (i + 1).ToString());  
  42.             }  
  43.         }  
  44.   
  45.           
  46.     }  
  47.   
  48.     //下载所有图片  
  49.     private void DownLoad()  
  50.     {  
  51.         for (int i = 0; i < _urls.Length; i++)  
  52.         {  
  53.             DownLoadAllImages(_urls[i], _localPath[i]);  
  54.         }  
  55.     }  
  56.   
  57.   
  58.     void DownLoadAllImages(string url, string localPath)  
  59.     {  
  60.         WebClient web = new WebClient();  
  61.         web.DownloadFile(url, localPath);  
  62.         //以下代码下载完成后执行  
  63.     }  
  64. }  


  65. 原文链接:

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值