- Home /
Why do multiple Unity coroutines speed up www image loading?
I googled my way through lots of questions all day but I didn't seem to find an answer fitting my problem, so I hope somebody can help me.
I want to display several image streams in a Unity scene. I have several GameObjects, each with the same loading-script attached and load my images via a coroutine in this script:
IEnumerator Load() { Texture2D tex = new Texture2D(4, 4, TextureFormat.DXT1, false); while (true) { WWW www = new WWW(url); yield return www; www.LoadImageIntoTexture(tex); img.texture = tex; } }
(Where img.texture is my image in the scene and url is the url i'm loading from).
This works fine for loading the images, but I notice the images load/ stream faster once I start more coroutines. To clarify, if i have 1 image stream it's updating the images at a certain speed but if i have, e.g. 3 image streams (each having a coroutine for loading) suddenly all 3 streams load images faster than with 1 stream.
I tried adding a yield return new WaitForFixedUpdate();
at the end of the coroutine but it didn't help, and i have no idea how to achieve a regular pace in loading new images, independent how many streams/coroutines i have at the same time? I hope it's clear what i mean and any help is appreciated!
Answer by FortisVenaliter · Jul 17, 2017 at 09:03 PM
I can't say for sure, but it's probably a question of whether you are loading sequentially or concurrently.
If you have three coroutines, do they all start at the same time? If so, they send their requests more or less at the same time, and get their responses back very quickly one after the other.
If you have one coroutine, then each has to wait for the previous to complete before it can start the next.
Even though the first method is faster when you have one user, though, it could cause problems if the web server is unable to keep up with the volume of many users. Using the second method in that situation would give it a chance to breathe and may increase stability.
There are 3 different coroutines but they don't start at the same time. The user can choose to display 1, 2, 3, .. image streams at the same time. Once he starts a stream a new gameobject with the image-loading-script is instantiated and the coroutine started.
So I noticed if i display 1 stream it's updating images at a certain speed but if i choose more streams the speed increases.. is this "normal" behaviour or can i regulate it somehow?
It could also be simple caching on the server side. $$anonymous$$ost servers are smart enough to know that if one file is requested, other files nearby will likely be requested too and cache those for easy transmission.
So, it could by that by requesting one, you're inadvertently telling the server "queue those others up, I may need them soon", so it's much faster after the first.
I see, so it doesn't have to do with the corooutines or with the way I am loading the images? Interesting, I didn't think of that.. So the way i'm handling this seems fine to you? Displaying image streams by starting 1 coroutine for each stream and looping in it? Thanks a lot!
Your answer
![](https://koobas.hobune.stream/wayback/20220612130513im_/https://answers.unity.com/themes/thub/images/avi.jpg)