Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 fschaar · Dec 10, 2015 at 08:34 PM · coroutineyieldwww classendless loop

www class blocks script execution

Ok, maybe this is a silly question: We are loading some files from disk and use www class for that und my colleague has used WWW class for that, but WITHOUT putting it into a coroutine. Instead he put a simple while Loop behind it, to wait until it ended (so the end is awaited in the current frame, where the loading was started).

   public static bool IsValidWWW(WWW www) {
       
         while (!www.isDone)
                ;
         return www != null && string.IsNullOrEmpty(www.error) && www.size > 0;
     }

The files are very small, so that doesn't make any problem and everything works fine in Editor, on Android and Standalone, but on iOS the device is blocked.

My Guess is, that www class uses a special amount of time in the frame for downloading, then stops and the next frame is loaded. On the other platforms it extends the time for download, but on iOS the Frame is clamped to 30 so the time es up, but the execution can't go on, because it is trapped in a while loop.

Or is www executed thread based?

I just want to be sure, how it works - because if this is the cause of the error, we have to change A LOT OF code, to make this coroutines based.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by callen · Dec 10, 2015 at 08:45 PM

It's surprising that this works for you as often as it does, (I even tested because I didn't believe it!) but I'm guessing the reason is threading. It is likely the case that the WWW operation can be offloaded to another thread on some platforms, but on iOS it needs to use the main thread, at least to start the operation.

In general however, you should never do this! I tested again in the editor with my internet disconnected, and sure enough the editor froze then crashed, because it was stuck in the loop.

There's a reason why every language's net APIs use Async methods and callbacks, and you will likely have to redesign along this pattern. To 'fix' your method you could write it like this:

 public static IEnumerator IsValidWWW(WWW www, Action<bool> callback) {         
          while (!www.isDone)
                 yield return null;
          callback( www != null && string.IsNullOrEmpty(www.error) && www.size > 0 );
      }

There's really no way around it, because there's never a guarantee a net operation can complete within any fixed timeframe. Especially on mobile, where weird stuff can happen in the OS and your WWW might not return for a few seconds.

Edit: i must have misread about loading local data via Www class. While that should work, the fact that you see this behavior makes me think to avoid it. Maybe something about the file:// scheme is operating on ios in a way that requires the frame to conclude before it completes (while other platforms dont)

The .net file io classes should be able to handle any of your needs though. It might be as simple as

if (File.Exists (path)) text = File.ReadAllText(path);

Comment
Add comment · Show 2 · 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 fschaar · Dec 10, 2015 at 09:12 PM 0
Share

Hmmm we are not loading from Internet with this - I made the Internet Downloads myself correctly with coroutines. $$anonymous$$y friend made that one for reloading it from disk. So it should either finish sometimes or throw an error, that the file doesn't exist. That's what he expected. But he doesn't know anything about coroutines. The interesting Thing is, that the download stops completely, but has started. Progress shows 0.5 but it stucks there, so I still wonder why and why it works on other systems.

avatar image fschaar · Dec 11, 2015 at 03:36 PM 0
Share

Yes, File.io operations was the next thing i was thinking about, but that offers another new problem. I download Image (png) and audio (mp3) files from the Internet (with www class inside a coroutine - everything fine), store it on the device und want to display or play it later in the game. i can read them as bytes with file.io and i can put images with loadImage into a texture to work with it, but i haven't found a way, to put byte mp3 data into a audioclip. So I'm stuck with that solution too ;(

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

32 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WWW yield is not returning any value in ios for yahoo finance api 2 Answers

Figuring out Coroutines 3 Answers

How do I return a reference to an object downloaded via a function? 0 Answers

How to make a loading feedback for big GameObjects loading? 2 Answers

While loop yield lag? 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