- Home /
Is www class working like a coroutine or like a Thread?
I still dont no how it works. If it is like a coroutine it means there is a time slot within each frame, where www makes a part of the Download, stops until the next Frame (yield) and continues the Download in the next frame. That also means the class has a way to determine how much time is available in each frame for the Download.
The other option is, that it works as a thread simultanusly to the Main (framebased) thread. Can anyone make that clear? Is the behaviour the same on each platform or does it work different eg. On iOS?
Answer by Bunny83 · Dec 11, 2015 at 04:12 PM
We simply don't know how the whole class is implemented since it's part of the native part of Unity. Also the implementation can be different on different platforms.
On windows it looks like the "download" is always handled / executed in a seperate thread, even local file IO. Apart from where the actual work is done, Unity will always have to synchronise the result with the main thread. This could already be done inside the isDone property, but it's also possible that it's handled elsewhere on the main thread.
From your question it seems you have a strange view what a coroutine is. There are no "timeslots". You have timeslots in a multithreading environment where the OS interrupts a thread after some time to let other threads proceed their work. Cooperative multitasking has nothing to do with time, at all. If one cooperative task blocks the execution inside a loop without yielding your whole thread will be stuck there.
A "download" so using actual sockets and the http protocol is always asynchron. You can't "stop" a download. A download is a passive process once initiated. The TCP/IP protocol stack as well as you NIC driver of the OS will handle the reception of the data. An application either uses callbacks to get informed when new data arrived or uses some sort of polling. So it checks if data is available at certain intervals.
File IO is in most operating systems a synchronous operation. So it will block until the operation is done. Of course it's always possible to do heavy IO operations in a seperate thread to prevent the blocking of the main thread.
In general i wouldn't recommend to use the WWW class for local file access and if you do, use a coroutine. For simple File IO it's way easier to use the System.IO
classes.
You most likely won't get a more precise or more detailed answer on that topic, so please don't ask the same question again. You're using a thrid party proprietary class so you can only rely on the documentation that comes with it. The documentation on IsDone clearly says that you shouldn't use a blocking loop. If you do it might work on some platforms but might fail on others.
Btw. You might want to read this:
http://forum.unity3d.com/threads/www-isdone-block-on-ios.105889/
Thanks for the link. So it is clear at last, that it won't work without a coroutine. That is bad news. So we have to change a lot of code. I also tried File.io Operations, but I'm loading Images and mp3 files, to put them into textures and audio clips. Images are no problem since there is the LoadImage() funktion, but i don't know how to get mp3 bytecode into an audioclip. So I'm stuck there to ...
Unity doesn't support loading mp3 files at runtime on any platform. Only on iOS and on Android you can use the OS native mp3 support to play one background strea$$anonymous$$g mp3. And yes, the only way is to use an mp3 on those platforms is to use the WWW class.
I once took a 100% managed mp3 decoder library and included it in Unity. I was able to load and decode mp3 files on any platform. However due to the mp3 patent madness i never published my attempt. Also i can't remember if the decoder was released under GPL which would force you to release your whole game under GPL as well. So all in all not worth the hassle.
So either use the mp3 strea$$anonymous$$g function which you can reach using the WWW class, or use a different file format which you can decode at runtime manually. If you have your own decoder you would simply use SetData to convert your PC$$anonymous$$ sample data into an AudioClip, or create an AudioClip and use the PC$$anonymous$$ReaderCallback to pass the decoded data to Unity.
Anyways you should get rid of that blocking while loop.
Thank you for the helpful advices. So I stop searching for an audio Reader (ogg would be an option to) and start building coroutines ;)
Answer by steakpinball · Dec 11, 2015 at 04:06 PM
The behavior is platform specific. Though the general idea is the same across all. You interact with the WWW class as though it is a coroutine. The actual implementation is in native code and uses threads.
For example, on iOS the default implementation uses Apple's NSMutableURLRequest and NSURLConnection. The iOS implementation is in the file WWWConnection.mm
in the iOS playback engine.