Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Icewing726 · Sep 05, 2016 at 08:40 PM · coroutinewwwdownload

Need Coroutine Download to finish first

Hi guys, maybe you can save me two days of crying. So here's my issue, I have a program that stores files server side that I need to be client side (lots of reasons for this). At times the number of files to download may be large, some times they may be small (all depends on if the server has the more recent file or not). That being said I have the download coroutine set up and the script works in so far as figuring out what files need to be downloaded, BUT, due to the way coroutines go (as if I really understand it) it doesn't finish downloading till the end of the script (which because I use variables to pass file paths means only one file eventually gets downloaded).

SO, in short I need to wait for each download to complete prior to moving on in the code. I've viewed several responses on this subject but none of them made sense to me and I was hoping someone could help guide me onto the answer.

     public void CompareLists()
     {
      //do a lot of stuff
                 if (LocalFileList.Contains(ComparisonText))
                 {
                   //do some more sorting
                     if (LocalFileDate <= AWSFileDate)
                     {
                         StartCoroutine( WaitForRequest());
                         Debug.Log("last");
                     }
                 }
                 else
                 {
                     StartCoroutine(WaitForRequest());
                     Debug.Log("last");
                 }
             }
         }
     }
  
     IEnumerator WaitForRequest()
     {
   //do some junk here like define url and filepath
             WWW www = new WWW(filepath));
             yield return www;
             File.WriteAllText(filepath,www.text);
     }

So like I said this whole thing works EXCEPT I need one coroutine to finish before moving on, I deleted a lot of fluff and logic to make the code string shorter. Thanks for your time.

Comment
Add comment · Show 4
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 Icewing726 · Sep 05, 2016 at 08:41 PM 0
Share

Just an update, while I'm at the mercy of getting this posted. I put a coroutine on the main thread that checks if the downloads finished. However this hasn't worked for me because Unity still process code downstream from the download requests (The methods downstream use the downloads to create prefabbed tiles, so no downloads means no tiles).

avatar image TBruce · Sep 05, 2016 at 08:55 PM 0
Share

Have you tried this approach

 private bool updatingFiles = false;  // are we in the process of updating files?
 
 public void CompareLists()
 {
     if (!updatingFiles)
     {
         StartCoroutine( UpdateFiles());
     }
 }
 
 IEnumerator WaitForRequest()
 {
     updatingFiles = true;
     for (int i = 0; i < LocalFileList.Count; i++)
     {
         // is the current file to be downloaded/updated
         if (LocalFileList[i].fileNeedToBeUpdated)
         {
             //do some junk here like define url and filepath
             // string filepath = whatever;
             WWW www = new WWW(filepath));
             File.WriteAllText(filepath, www.text);
             while (www.progress < 1.0f)
                 yield return www;
         }
         yield return null;
     }
     yield return null;
 
     updatingFiles = false;
 }
avatar image Icewing726 TBruce · Sep 05, 2016 at 09:35 PM 0
Share

In hindsight I can see that its really important to mention that the file name is being pulled by a foreach(string fileanddate in whateverlist). So that means several coroutines are being created in parallel, but the key variable fileanddate is being updated. That being said the method you proposed wouldn't work for me because it would simply skip downloading a file as it iterates through the list. Thanks for the reply though.

The only solution I can think of is a way to download files without it being a background operation or a way to hold the program till the coroutine finishes.

Or I could start doing work around 3 if theres a way to make a coroutine store an instance of the variable (Probably didn't use that term right, kind of like saying corticosteroid in a medical conversation).

avatar image Icewing726 Icewing726 · Sep 06, 2016 at 01:07 AM 0
Share

<=== Noob may or may not have the answer to this. After doing a lot of reading one line started to stand out. yield return gives control back to unity. $$anonymous$$oving yield return null to the end of the coroutine statement got me some good errors of not done downloading yet, with some helpful while != wait command suggestions. This may solve my problem.

1 Reply

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

Answer by Icewing726 · Sep 06, 2016 at 01:50 AM

Here's how you wait for a download. 1) instead of following every example out there that says to yield return www right after calling WWW www, put yield return null at the end. 2) Add the code: while(!www.isDone){} so that it waits for the isdone to do anything with the information you just downloaded. (probably want to add a timeout for this)

Feedback is still welcomed, I'm a noob afterall.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

IEnumerator skips remaining function after yield 1 Answer

Asset Bundle not compatible between different unity versions 1 Answer

When to use IEnumerator ? 1 Answer

How to Download file (Texture) in runtime and use it in script? 0 Answers

Issues with video player coroutine 2 Answers


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