Search Unity

Question How to download data while Android/iOS app is minimized or put into background?

Discussion in 'Addressables' started by Peter77, Aug 16, 2021.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Unity 2019.4.20f1, Addressables 1.18.4, Android Player, iOS Player

    We use
    Addressables.DownloadDependenciesAsync
    to download bundles from Unity Cloud Content Delivery.

    Minimizing or putting the app into background pauses the download. Bringing the app into foreground resumes the download again.

    Is there a way to continue downloading the content while the app is minimized or put into background?
     
  2. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    I recently solved this exact problem by implementing custom Bundle provider. To my knowledge, there is no way you can continue download when app is minimized. It may even be unloaded entirely. To support this out of the box, Unity needs to add some magic to UnityWebRequest and use some kind of system service for background downloading.

    In my case I solved this by using DownloadManager class natively on Android. (no solution for iOS yet - there was no need)
     
    Petr777 and Peter77 like this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    I would actually like to hear some thoughts from the developers on it. Is such feature planned? From an users perspective, it's not a great experience to require to keep the game in the foreground to download the files. @TreyK-47
     
    phobos2077 likes this.
  4. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    218
    As far as I'm aware the app is allowed a certain amount of time to continue to do things while in the background. But its only <20 sec as far as I'm aware.
    I have a multiplayer game and you can minimise the app and reopen it within that time and the sockets will stay connected, any longer than that and they dissconnect due to the OS killing all activity.
    So unless your download can complete in that time, I doubt there is any way to do it without running a background service and even then you are very limited on what you can do.
     
    phobos2077 likes this.
  5. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,820
    I'll forward this over to the team, see if they have some guidance to share.
     
    phobos2077 likes this.
  6. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    972
    For the record, if you want to implement this yourself in the addressables system you can use com.unity.networking.backgrounddownload. It abstracts away the differences between iOS and Android.
     
  7. andymilsom

    andymilsom

    Unity Technologies

    Joined:
    Mar 2, 2016
    Posts:
    294
    It looks like others have provided the answers, so I will just confirm.

    Addressables uses UnityWebRequest which has no resume or background download functionality.
    For iOS and Android you need to use native download methods which the package @AlkisFortuneFish mentioned handles.
    In order to implement this, you will need a custom AssetBundleProvider that uses that to download (Take note that you remember to do caching and recompression).
    We currently have no schedule to move from UnityWebRequest add this functionality at this time.
     
    Opeth001 likes this.
  8. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,116
    Since this is something in highly required for mobile platforms, can you please provide a small example or a documentation link on how to do it?
    Thanks
     
  9. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    902
    Is there any documentation for guidance on implementing this?