- Home /
How to download in the background using WWW class?
Hi Guys!
I am currently evaluating Unity Pro and I just made a Patcher C# script. It does the basic thing of a patcher like CRC checks etcetera but my problem is, the processes are blocking. I need a download of WWW be asynchronous in such a way I can show an animated progress or waiting popup.
Any thoughts on how to achieve this the Unity way?
Any way to do threads for instance? Are C# threads supported?
Answer by Kryptos · Jan 12, 2012 at 10:29 AM
Well. WWW class is a Coroutine so you can just yield it until it is completed. http://unity3d.com/support/documentation/ScriptReference/WWW.html
For example:
bool isDone = false;
void IEnumerator Start() {
WWW myW = new WWW( url );
yield return myW;
isDone = true;
}
Or using the isDone and progress properties:
float progress = 0.0f;
void IEnumerator Start() {
WWW myW = new WWW( url );
while( !myW.isDone )
{
progress = myW.progress;
yield return null;
}
progress = 1.0f;
}
Hi thanks for the reply. I saw that reference before I just don't understand it. First, does it always have to be in Start() or Awake()? $$anonymous$$y Api for my patcher is like: public int DownloadFile( string p_urlDlc, string p_localFilename ) It returns an enumeration like Busy, Fail, Free. It can be called on other script anywhere like inside Update or FixedUpdate.
So depending on how I can do "threading" my DownloadFile function prototype will be changed (polled vs some other way). Any suggestion would help.
Answer by Eric5h5 · Jan 13, 2012 at 02:01 AM
WWW already downloads in the background and doesn't block anything. It's inherently asynchronous, hence the isDone and progress variables. If your code blocks it's only because you programmed it that way; typically you use WWW in a coroutine. (Although not necessarily--you could constantly poll the isDone variable in Update for example, but it's usually much simpler and easier just to yield on the result.)
As for the question about threads, you can use threads in Unity, but you can't access any of the Unity API outside the main thread, so you can't use threads for the WWW class. Not that you need to.
Would like to know if I quit the current scene which the downloading process occurred, will the downloading process been kill or stop?!
"Would like to know if I quit the current scene which the downloading process occurred, will the downloading process been kill or stop?!" ... it's tricky. Here's a discussion: http://answers.unity3d.com/questions/462462/is-it-actually-true-that-stopallcoroutines-stops-w.html
It's only very trivial applications that decide they need to download data in a monobehavior's events. Normally it's buried down inside some cache manager or other background process.
The only option is to make the decision elsewhere and expose it to a pointless $$anonymous$$onobehavior so it can manage the download.
$$anonymous$$akes it impossible to architect a clean design.
Answer by jema · Jul 09, 2014 at 09:42 PM
Try to use WWW wrapper:
UCSS.HTTP.GetTexture(textureAddress, new EventHandlerHTTPTexture(OnTextureDownloaded));
It uses callbacks, https://www.assetstore.unity3d.com/en/#!/content/19116
Answer by liortal · Dec 07, 2018 at 12:17 PM
You can use a coroutine to download data using the WWW class, without blocking your game from running other things.
If you'd like to download data in the background (even while your game is not running), check out this plugin on the asset store: http://u3d.as/1jqN
Your answer
![](https://koobas.hobune.stream/wayback/20220613062916im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Download images from server and storing it in Resources. 1 Answer
How to import wav file trough script from Project folder? 0 Answers
WWW doesn't work outside of editor. 1 Answer