- Home /
Two yield or not two yield (pattern?)
Sorry for the lame subject, but I am having troubles with yield while loading some bytes from a WWW object in C#.
Task
Load multiple files with WWW from a webserver, from a UnityPlayer webclient.
But I need to wait for EVERY file, until the game can start.
Some of the files are leveldata telling how to build the level others are players and NPC data etc.
And everything is needed before the simulation/game may start.
Question
Should I spawn multiple WWW objects and yield them all or only one at the time? The server I am loading from could be different addresses (CDN's) so speed is not the problem. My problem is only what pattern is recommended and why and ... ouch! HOW?!
I have a yield running now, loading 1000 bytes from a webserver, it returns the 1000 bytes (sooner or later) but everything starts, so I will need to make some kinda state-machine globally too I guess.
I think I will yield until someone can answer this.
It doesn't really matter because its all done on the main thread. So they will be done one at a time no matter how you slice it.
okay, but I still need to wait for them and then be able to use the data from them afterwards. They load text, bytes etc.
Answer by Peter G · Jul 19, 2011 at 01:13 AM
You can spawn them all at one time, but you probably won't save much time since Unity isn't multithreaded. Only one will ever be loading at any given time so your speed probably won't be noticeably different. It shouldn't change if one loads fully then you load the next one or if you load a little bit of each during each frame. Personally I would put it in a loop and load each one after the next just because it looks nice:
for(var i = 0; i < URLS.Length; i++) {
URLS[i] = new WWW(addresses[i]);
yield return URLS[i];
//do something else with the data;
}
If you know .Net sockets you might be able to do something similar and then push that onto different threads.
that makes sense, but where do you store the data then or how do you index the found data?
Do I access the array of URLS (as a global array) after the FOR loop?
Yes. The info will still be there URLS[i].text
or whatever you need.
You also might want to be concerned that you are allocating a lot of memory in a very short amount of time by doing it this way. Why can't you stream them in as you need them?
Its not a big memory issue as the objects and lists arent very long, I just need them all before I can create the level. Its a one way only level and a lagspike would ruin the competition part of it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150031im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to create a static class for WWW-based functions and such 0 Answers
Custom yield return 1 Answer
How to set timer for WWW helper? 1 Answer
How can I have a while(www is loading) loop? 1 Answer
yield return request never returns 2 Answers