- Home /
Background Networking Activity and Unity Run Loop
We are looking for a way to download "concurrently in the background" various resources that our game will need "soon". Our requirements are (1) maximum possible download speed, (2) minimum possible disruption of "foreground" Runloop activities that are driving the game, (3) the solution works extremely well on all the major platforms of interest to us (Android/iOS/PC/Mac), and (4) absence of bugs that debilitate the solution.
If we don't meet requirement #1, then the game has to stop and make the user wait until the background download catches up and delivers the requisite resources. If we don't meet requirement #2, then the background download negatively impacts how the game plays, making it jerky or slow-to-respond, etc.
We've tried and investigated various approaches, including Asset Bundles, WWW, and native C# HttpWebRequest, and non of them meet all of our requirements. Details below.
We'd love to chat with you if you've already figured out how to solve this problem, because we'd rather not reinvent the wheel.
Here are the details of our experience so far:
We have an Android app built in Unity - eventually to also be available via iOS. Alongside the normal foreground activities, we have a background low priority activity of downloading various resources to be used by the app in the foreground.
(Before you ask, Asset Bundles will not work for us as - unless anything has changed recently - they do not work across different versions of Unity - i.e. an asset bundle for Unity 3.4 does not work in Unity 3.5. So an upgrading user will have to re-download all their assets all over again.)
Unfortunately, we have discovered a number of annoying bugs with using the 'WWW' component. These include not updating when there is a loss of network, and no return values from web requests (i.e. 'post'). As such, we are looking to replace our use of WWW with the native C# HttpWebRequest. Unfortunately, we have found using this component in the main Unity Runloop via a co-routine causes the runloop to stall and the UI to freeze.
But placing this same code in a background C# thread (not a co-routine) and then polling that thread's activity does seem to work.
We do not like this approach as this does allow us to schedule this activity in away which is in harmony with the Unity scheduler. We would like the network (and associated CPU) activity to be run in appropriate gaps in the Unity Run Loop. But as far as we can tell, there are only three ways we can specify priority:
WWW.threadPriority - we do not want to use WWW so this is mute.
Application.backgroundLoadingPriority - As we are not using a Unity component (like AssetBundles) to do the downloading, I am assuming this has no effect?
YieldInstructions e.g. WaitForSeconds and WaitForFixedUpdate - My understanding is that these are fairly dumb. They run when they are scheduled to run and do not defer to the needs of the Unity Run Loop?
What we are looking to understand: - Is WWW really fraught with problems or is this just our use of the component? - Has anyone used HttpWebRequest w/ a Unity app successfully? Have they done so in a threaded fashion? - Is there any 'interrupt' or 'priority' architecture in Unity so that background tasks can play nicely w/ the Unity Runloop and not cause glitches or competition?
Any help here is much appreciated!
Thanks, Tim
Your answer
Follow this Question
Related Questions
Get the WiFi IP address of the device 1 Answer
Connecting multiple platforms to one server 2 Answers
Keep network connection from interrupting on iOS multitasking switch 1 Answer
Smartphone multiplayer - NAT problem? 2 Answers
Does UnityEngine.Ping currently support ipv6 addresses on mobile devices? 0 Answers