Unity 使用Unirx网络库进行文件下载
来源:互联网 发布:网络兼职被骗怎么办 编辑:程序博客网 时间:2024/05/29 08:13
Unirx地址:https://github.com/neuecc/UniRx
下面是进行文件的下载的两个方式,单个下载和批量下载。包括加入请求头
using UnityEngine;using System.Collections;using UniRx;using System;using System.IO;using System.Collections.Generic;public class DownloadManager{ public static Download Download(string url, string path, Action<float, string> progress, Action<string, Exception> onError = null, Action<string> onComplete = null, Dictionary <string,string> header = null) { FileStream stream = null; IDisposable disposable = ObservableWWW.GetAndGetBytes(url, header, new Progress<float>(progress, url)).Subscribe((byte[] bytes) => { if (null == stream) { stream = File.OpenWrite(path); } if (null != stream) { stream.Write(bytes, 0, bytes.Length); } }, (Exception e) => { if (null != stream) { stream.Flush(); stream.Close(); } if (File.Exists(path)) { File.Delete(path); } if (null != onError) { onError(url, e); } }, () => { if (null != stream) { stream.Flush(); stream.Close(); } if (null != onComplete) { onComplete(url); } }); Download download = new Download(); DownloadItem item = new DownloadItem(url, path, disposable); download.AddDownloadItemToQueue(item); return download; } public static Download DownloadAll(string[] urls, string[] paths, Action<float, string[]> progress, Action<string[], string[]> onComplete = null, Dictionary<string, string> header = null) { Download downloads = new Download(); int length = urls.Length; Dictionary<string, float> totalProgress = new Dictionary<string, float>(); List<string> success = new List<string>(); List<string> errors = new List<string>(); for (int i = 0; i < length; i++) { totalProgress[urls[i]] = 0; } int respondCount = 0; System.Object lockObj = new System.Object(); for (int i = 0; i < length; i++) { Download download = Download(urls[i], paths[i], (float value, string url) => { totalProgress[url] = value; if (null != progress) { float curTotalProgress = 0; for (int j = 0; j < length; j++) { curTotalProgress += totalProgress[urls[j]]; } progress(curTotalProgress / length, success.ToArray()); } }, (string url, Exception e) => { lock(lockObj) { respondCount++; } errors.Add(url); if (respondCount >= length) { onComplete(success.ToArray(), errors.ToArray()); } }, (string url) => { lock(lockObj) { respondCount++; } success.Add(url); if (respondCount >= length) { onComplete(success.ToArray(), errors.ToArray()); } }, header); downloads.AddDownloadToQueue(download); } return downloads; }}public class Progress<T> : IProgress<T>{ private Action<T, string> progress; private string url; public Progress(Action<T, string> progress, string url) { this.progress = progress; this.url = url; } public void Report(T value) { if (null != this.progress) { this.progress(value, url); } }}public class Download{ private Queue<DownloadItem> downloadItemQueue = null; public void AddDownloadItemToQueue(DownloadItem item) { if (null == item) { return; } if (null == downloadItemQueue) { this.downloadItemQueue = new Queue<DownloadItem>(); } this.downloadItemQueue.Enqueue(item); } /** * 合并两个下载,把一个下载里面的任务合并到另外一个下载 */ public void AddDownloadToQueue(Download download) { if (null == download) { return; } if (null == this.downloadItemQueue) { this.downloadItemQueue = new Queue<DownloadItem>(); } for (int i = 0, count = download.downloadItemQueue.Count; i < count; i++) { DownloadItem item = download.downloadItemQueue.Dequeue(); this.downloadItemQueue.Enqueue(item); } } public void Cancel() { if (this.downloadItemQueue == null) { return; } for (int i = 0, count = this.downloadItemQueue.Count; i < count; i++) { DownloadItem item = this.downloadItemQueue.Dequeue(); item.disposable.Dispose(); if (File.Exists(item.path)) { File.Delete(item.path); } } }}public class DownloadItem{ public string url; public string path; public IDisposable disposable; public DownloadItem(string url, string path, IDisposable disposable) { this.url = url; this.path = path; this.disposable = disposable; }}
阅读全文
0 0
- Unity 使用Unirx网络库进行文件下载
- ios开发网络篇 - 使用ASI框架进行文件下载
- 使用数据流进行文件下载
- 使用DownloadManager进行文件下载
- 使用Struts2进行文件下载
- android使用网络下载文件
- 网络:使用多线程下载文件
- vpn下使用原有网络进行下载
- iOS开发网络篇—使用ASI框架进行文件下载
- iOS开发网络—13使用ASI框架进行文件下载
- iOS开发网络篇—使用ASI框架进行文件下载
- iOS开发网络篇—使用ASI框架进行文件下载
- 使用jspSmartUpload组件进行文件上传、下载
- 01-使用struts进行文件下载
- 使用FileSystem 进行文件上传和下载
- 使用DWR3.0进行文件的下载
- 使用ASI框架进行文件下载
- 使用ASI框架进行文件下载
- linux nand bbt 坏块管理部分(转
- JAVA用户注册登录使用MD5非对称加密解密
- 根据ID集合查询符合某种类型的用户信息,并按其他类型分类
- ECharts学习篇一
- java NIO(non-blocking I/O) 非阻塞式io
- Unity 使用Unirx网络库进行文件下载
- 小游戏制作2
- html页面之间的数据传递
- 初识Hadoop之Hadoop单机版搭建
- Contruct 2制作的HTML5游戏的简单介绍
- LeetCode Exercise 10: Merge Intervals
- 语义分析-哈工大编译原理第三次实验
- 关于重载new delete new[] delete[]笔记----C++学习之路
- 约束