Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nubee_Andrei · Jan 12, 2012 at 10:08 AM · c#www

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nubee_Andrei · Jan 12, 2012 at 09:49 AM 0
Share

Any way to do threads for instance? Are C# threads supported?

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nubee_Andrei · Jan 13, 2012 at 01:45 AM 0
Share

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.

avatar image
3

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ina · Apr 15, 2012 at 08:07 PM 0
Share

how do you use threads for WWW?

avatar image sinergy · Oct 15, 2013 at 02:40 AM 0
Share

Would like to know if I quit the current scene which the downloading process occurred, will the downloading process been kill or stop?!

avatar image Fattie · Mar 13, 2014 at 08:59 PM 0
Share

"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

avatar image Simon-O · Dec 14, 2016 at 01:07 AM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges