I’m trying to fetch data from a server that can range anywhere from 100kb to 15 mb. The download speeds are reasonable for files <5mb, but after that it becomes super slow. An 11mb file took 4.5 minutes to download. Is the download buffer size for the WWW class just super small? Is there an alternative?
At first I thought it was maybe because I was yielding every frame so the file download was going to be interrupted every few ms to yield to the frame. So I made it yield only when the progress increased by 5%. This sped things up a little bit but it’s still awfully slow.
var progressCounter = .05;
while (!www.isDone) {
if(www.progress/progressCounter > 1) {
progressCounter += .05;
yield return null;
}
}
WWW is run in a separate thread, not on the main thread.
WWW allows you to specify the threadPriority (Low, BelowNormal, Normal, High)
Yield is a function of Unity’s coroutines. Coroutines are not threads, all it does is save the current state of the function and wait a frame and resumes from the line after the yield to execute as normal. Your code is blocking the main thread by performing while( !www.isDone), and waiting till 5% is downloaded to yield.
This could have a very adverse affect on your frame-rate. The main thread is also the UI/rendering thread. Using this approach the main thread is dependent on a set of outside resources (internet connect, signal strength, bandwidth, server availability, sever upload speeds, etc.) If the WWW thread is low priority, the WWW thread would starve. If the WWW thread is a high priority then this may be ok or speed up download, but at the cost of starving your main thread. The higher the priority for the WWW thread the more your frame-rate will suffer as more of time is spent in that thread.
There are a lot of reasons for slow downloads. Verify that it is not an outside source. Compare download speeds on WiFi vs a Cellular. Connect to a local server for downloads. Make sure that there aren’t any background services on the device downloading during your tests. Try and use a web proxy like Charles or Fiddler to observe or throttle download speeds. A proxy can also show you all the activity that is occurring on that device.