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

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;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值