前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity中使用WWW封装下载类管理代码片段

Unity中使用WWW封装下载类管理代码片段

作者头像
bering
发布2019-12-03 15:53:56
6010
发布2019-12-03 15:53:56
举报
文章被收录于专栏:游戏开发之旅

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/CJB_King/article/details/89315810

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DownloadManager : MonoBehaviour , System.IDisposable {
	// using classes
	public class Request {
		public string url;
		public DownloadDelegate del;
		
		public WWWForm form;
		
		public byte[] bytes;
		public Hashtable header;
		
		//-------------------------------------------------------------
		// Constractors
		//-------------------------------------------------------------
		public Request(string url, DownloadDelegate del) {
			this.url = url;
			this.del = del;
		}
		
		public Request(string url, DownloadDelegate del, WWWForm form) : this(url, del) {
			this.form = form;
		}
		
		public Request(string url, DownloadDelegate del, byte[] bytes) : this(url, del) {
			this.bytes = bytes;
		}
		
		public Request(string url, DownloadDelegate del, byte[] bytes, Hashtable header) : this(url, del, bytes) {
			this.header = header;
		}
		
		//
		public WWW MakeWWw() {
			if(header != null) {
				return new WWW(url, bytes, header);
			}
			if(bytes != null) {
				return new WWW(url, bytes);
			}
			if(form != null) {
				return new WWW(url, form);
			}
			return new WWW(url);
		}
	}
	
	class TimeOut {
		float beforProgress;
		float beforTime;
		
		public bool CheckTimeout(float progress) {
			float now = Time.time;
			if((now - beforTime)>TIME_OUT) {
				// timeout
				return true;
			}
			// update progress
			if(beforProgress != progress) {
				beforProgress = progress;
				beforTime = now;
			}
			return false;
		}
	}
	
	// delegates
	public delegate void DownloadDelegate(WWW www);
	public delegate void DownloadErrorDelegate(string error);
	public delegate void DownloadFinishDelegate();
	
	// const
	public const float TIME_OUT = 20f;
	
	// public 
	public event DownloadErrorDelegate OnError;
	public event DownloadFinishDelegate OnFinish;
	
	// member
	Queue<Request> requests;
	WWW _www;
	bool isDownloading = false;
	TimeOut timeout;
	
	// static
	private static DownloadManager _instance;
	static public DownloadManager instance {
		get {
			if(_instance == null) {
				GameObject go = new GameObject();
				go.name = "DownloadManager";
				_instance = go.AddComponent<DownloadManager>();
				DontDestroyOnLoad(go);
			}
			return _instance;
		}
	}
	
	//-------------------------------------------
	// life cycle
	//-------------------------------------------
	void Awake () {
		requests = new Queue<Request>();
		timeout = new TimeOut();
	}
	
	void Destroy() {
		this.Dispose();
		_instance = null;
	}
	
	public void Dispose() {
		isDownloading = false;
		StopAllCoroutines();
		if(_www != null) {
			_www.Dispose();
		}
		requests.Clear();
		OnError = null;
		OnFinish = null;
	}
	
	void FixedUpdate(){
		if(!isDownloading) {
			this.enabled = false;
		}
		
		if(timeout.CheckTimeout(CurrentProgress)) {
			// timeout
			if(OnError != null) OnError("timeout");
			this.Dispose();
		}
	}
	
	//-------------------------------------------
	// public
	//-------------------------------------------
	public void Push(Request req) {
		requests.Enqueue(req);
	}
	
	public void Excute() {
		if(isDownloading) {
			Debug.Log("aleady downloading...");
			return;
		}
		StartCoroutine("Download");
	}
	
	public float CurrentProgress {
		get {
			if(_www == null) { return 0f;}
			return _www.progress;
		}
	}
	
	//-------------------------------------------
	// private
	//-------------------------------------------
	IEnumerator Download() {
		if(requests.Count == 0) {
			Debug.LogWarning("no requests");
			return true;
		}
		this.isDownloading = true;
		this.enabled = true;
		
		while(requests.Count>0) {
			Request req = requests.Dequeue();
			_www = req.MakeWWw();
			yield return _www;
			// error check
			if(_www.error != null && _www.error.Length > 0) {
				if(OnError != null) OnError(_www.error);
			}
			// call delegate
			req.del(_www);
		}
		
		if(OnFinish != null) OnFinish();
		this.isDownloading = false;
		this.enabled = false;
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档