- Home /
Question by
Rustam-Ganeyev · Jul 22, 2011 at 04:17 PM ·
c#wwwstartcoroutine
WWW DownloadManager, C#
Hi, i'm implementing something like download manager to unity using WWW class. I want to download and use things in order, as they came to me. Here's my code:
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections;
public class DownloadManager : MonoBehaviour
{
private class Downloadable
{
public string url { get; set; }
public DownloadCallback fn { get; set; }
public Downloadable (string url, DownloadCallback fn)
{
this.url = url;
this.fn = fn;
}
}
private WWW wwwData;
public delegate void DownloadCallback (WWW wwwData);
private static DownloadManager dm = null;
private static Queue<Downloadable> queue;
// Use this for initialization
void Start ()
{
if (DownloadManager.dm == null) {
DownloadManager.dm = FindObjectOfType (typeof(DownloadManager)) as DownloadManager;
}
queue = new Queue<Downloadable> ();
}
void FixedUpdate ()
{
if (queue.Count == 0 || (wwwData != null && wwwData.isDone == false)) {
return;
}
Downloadable first = queue.Dequeue ();
StartDownload (first.url, first.fn);
}
void OnApplicationQuit ()
{
DownloadManager.dm = null;
}
private IEnumerator WaitForDownload (DownloadCallback fn)
{
yield return wwwData;
fn (wwwData);
}
private void StartDownload (string sURL, DownloadCallback fn)
{
try {
Debug.Log ("start download: " + sURL);
wwwData = new WWW (sURL);
StartCoroutine ("WaitForDownload", fn);
} catch (Exception e) {
Debug.Log (e.ToString ());
}
}
public static void Download (string sURL, DownloadCallback fn)
{
Debug.Log ("added to queue: " + sURL);
queue.Enqueue (new Downloadable (sURL, fn));
}
}
But StartCoroutine ("WaitForDownload", fn); in StartDownload function starts not when previous thing finishes downloads,but immediately. How to fix that problem?
Thanks.
Comment
Best Answer
Answer by Rustam-Ganeyev · Jul 25, 2011 at 11:09 AM
It seems that I found solution. I used StartCoroutine function not in good way. Here's right version:
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections;
public class DownloadManager : MonoBehaviour
{
private class Downloadable
{
public string url { get; set; }
public DownloadCallback fn { get; set; }
public Downloadable (string url, DownloadCallback fn)
{
this.url = url;
this.fn = fn;
}
}
private WWW wwwData;
public delegate void DownloadCallback (WWW wwwData);
private static DownloadManager instance = null;
private static Queue<Downloadable> queue;
// Use this for initialization
void Start ()
{
if (DownloadManager.instance == null) {
DownloadManager.instance = FindObjectOfType (typeof(DownloadManager)) as DownloadManager;
}
queue = new Queue<Downloadable> ();
}
void FixedUpdate ()
{
if (queue.Count == 0 || (wwwData != null && wwwData.isDone == false)) {
return;
}
StartCoroutine ("StartDownload");
//StartDownload();
}
void OnApplicationQuit ()
{
DownloadManager.instance = null;
}
private IEnumerator OnDownload(Downloadable toDownload) {
wwwData = new WWW (toDownload.url);
yield return wwwData;
}
private IEnumerator StartDownload ()
{
Downloadable toDownload = queue.Dequeue();
yield return StartCoroutine("OnDownload", toDownload);
Debug.Log("downloaded: " + toDownload.url);
toDownload.fn (wwwData);
}
public static void Download (string sURL, DownloadCallback fn)
{
queue.Enqueue (new Downloadable (sURL, fn));
}
}