您的位置:首页 > 移动开发 > Unity3D

Unity3D实现批量下载图片功能

2018-07-20 08:46 549 查看

本文实例为大家分享了Unity3D实现批量下载图片功能的具体代码,供大家参考,具体内容如下

下一篇文章试试用线程下载

代码如下

using System.IO;
using UnityEngine;
using System.Net;
using System.Collections;
public class Test : MonoBehaviour {
private string[] _urls=new string[10];
private string [] _localPath = new string[10];
// Use this for initialization
void Start ()
{
for (int i = 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";
}
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 30), "下载所有图片"))
{
DownLoad();
}
//判断文件是否已下载
for (int i = 0; i < _urls.Length; i++)
{
if (File.Exists(_localPath[i]))
{
GUI.Button(new Rect(0, 30 * i+30, 50, 30), (i + 1).ToString());
}
}
}
//下载所有图片
private void DownLoad()
{
for (int i = 0; i < _urls.Length; i++)
{
DownLoadAllImages(_urls[i], _localPath[i]);
}
}
void DownLoadAllImages(string url, string localPath)
{
WebClient web = new WebClient();
web.DownloadFile(url, localPath);
//以下代码下载完成后执行
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Unity3D 下载