Created
April 25, 2012 14:32
-
-
Save asus4/2490117 to your computer and use it in GitHub Desktop.
Unity DownloadManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
/** | |
DownloadManager | |
@author koki ibukuro | |
*/ | |
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; | |
} | |
} |
タイムアウトも実装したのです!
how to download
In NetworkManager
line 170, it should be:
yield return true;
instead of return true
, as IEnumerator
cannot return result directly.
Almost forget to mention, Nice work !
Hi, thanks for a good work. Hashtable is obsolete and a Dictionary<string,string> could be used for Hader instead.
Regards
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE
OR