We are using the WWW class to download game data (in JSON format).
The data is downloaded using coroutines, and we are downloading multiple files at the same time (multiple coroutines are running).
I was wondering whether there’s a limit to the number of simultaneous requests we can have running, and whether that answer changes between different platforms ?
But still, the number of in-flight requests can get to 30-100 downloaded files. Is there any balancing going on from Unity’s side or from the OS side perhaps that you know of ?
I don’t think there’s any internal limit to the number of WWW requests you can do, but they are expensive from a memory standpoint, and older mobiles are going to be more of an problem on that front. So try to reuse your WWW objects, don’t make a new one for each call, and set it to null when the call is done. Also try to offset them in time a bit so they don’t happen simultaneously. An alternative is to make a socket connection with socket.io (check out UniWeb or BestHTTP for this), and send everything down one connection…
You should create a service object with a request queue. This object will take the desired amour of requests from queue and call the appropriate callback on request completion. After the callback has worked it pulls another request from the queue and continue. The code for issuing the request then might look like this:
var http = FindObjectOfType<HttpClient>();
http.Enqueue("http://my.request.url/...", (error, data) => {
if(error != null)
{
// handle the error here
}
else
{
// handle the response here
}
});
No need to use coroutines (or only one for pulling requests from queue), but if processing requests in separate threads you must ensure the callback is called on the main thread, the same one the main game loop is running.
Take into account what any code below this will be executed before the request is completed, immediately after enqueueing it.
The maximum number of requests then can be made available in the inspector as standard object property or adjusted automatically based on platform/internet speed/whatever. The WebClient instance can be cached in Start handler.