Search Unity

Question How to abort DownloadDependenciesAsync?

Discussion in 'Addressables' started by Peter77, Dec 1, 2021.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    Unity 2019.4.20f1, Addressables 1.19.9

    How can I abort a running
    Addressables.DownloadDependenciesAsync
    operation?
     
    Last edited: Dec 3, 2021
    phobos2077 and sozcan like this.
  2. su9257

    su9257

    Joined:
    Jun 13, 2017
    Posts:
    27
    I do need to abort the download operation for network or other reasons. This seems to be a lingering issue that Unity has never resolved.
     
  3. CineTek

    CineTek

    Joined:
    Jul 12, 2013
    Posts:
    98
    Also interested in this, but as a FYI - other games including e.g. Pokemon Unite do not pause their dynamic CDN downloads on iOS even if the user switches from WIFI to mobile even though it SHOULD either interrupt/pause or at least prompt the user.. at least according to Apple specifications
     
  4. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    If you don't want to wait (like, forever) for Unity to implement this obviously needed feature, you can try to do it yourself via custom asset bundle provider. Either try using the Unload method (which *should* be called when operation is released), or just write your own download management layer and add downloads to it from custom provider, then abort directly via some static member.
     
    Peter77 likes this.
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
  6. Gustavo-Santos

    Gustavo-Santos

    Joined:
    May 4, 2014
    Posts:
    13
    Currently im working in handling cancellation and restart of some tasks related to managing remote resources using addressables and unfortunately, looks like we cant cancel any Addressable API called.
    All Async methods from Addressables does not accept a CancellationToken as a argument
    So I tought, theres two more ways I can try to cancel this task:
    1 Running this operation in a coroutine, and stopping the coroutine
    2 Releasing the handle returned by the API
    For both cases I tried running a download, and cancelling it.
    I started the download, and after x seconds I downloaded 50MB/200MB, and then I tried cancelling the task by both, releasing the handle and stopping the coroutine
    after more x seconds I started again the same task to download the assets
    But now, it started from 100MB/200MB
    So looks like the download continued, do we have any confirmation of this behaviour from unity?
     
  7. unity_320AC700C5AB25D195F1

    unity_320AC700C5AB25D195F1

    Joined:
    Nov 19, 2021
    Posts:
    22
    Hit this exact same issue using Unity 2021.3.24f1 with Addressables 1.21.19.

    Using Addressables 1.20.05 if we try to cancel the download by stopping the coroutine calling DownloadDependenciesAsync and releasing the AsyncOperationHandle handle, DownloadDependenciesAsync would continue to download assets asynchronously in the background but at least the downloaded assets would be loaded into the scene.

    However, with 1.21.19 the same repro steps result in the downloaded assets not being loaded into the scene and it's not clear to me why this behaviour has changed. When I say "not being loaded" I mean I get this exception logged:

    "System.Exception: Unable to load dependent bundle from location <assetname>" from calling LoadAssetAsync.

    FWIW, interestingly if I pass true to the autoReleaseHandle param for DownloadDependenciesAsync (and comment out all the code using the DownloadResult) then the assets load correctly after stopping and restarting the coroutine.

    I can only imagine there's a difference in functionality of Release or recalling DownloadDependenciesAsync has introduced side-effects between versions. Anyone else found this discrepancy? Something to do with this change in 1.21.15 maybe: "DownloadDepedenciesAsync no longer loads asset bundles into memory"

    For now I'm just removing the functionality to allow users to cancel the download. I had considered creating a custom Asset Bundle Provider to abort the UWR created from DownloadDependenciesAsync when cancelling the download (as suggested in a number of posts by @phobos2077) but can't seem to find any reference material on what's required to do so.
     
    Last edited: Dec 20, 2023
  8. unity_320AC700C5AB25D195F1

    unity_320AC700C5AB25D195F1

    Joined:
    Nov 19, 2021
    Posts:
    22
    Just to follow-up on my comment above in case it helps others (namely @Gustavo-Santos as it sounds like we have the same use case). Through the combination of my findings it dawned on me that there must be something wrong with the way we're handling releasing the async operation handle. Indeed, since we were using a type derived from IDisposable to manage scope of AsyncOperationHandle in a 'using' block within the coroutine, the call to Release wasn't being made when stopping the coroutine (see thread here for ref)!

    Having changed our ref to the running coroutine from type Coroutine to IEnumerator and calling Dispose after stopping, our assets load just fine. Since our game code hasn't changed between Addressable versions, presumably this was always happening but the symptom of assets not loading has just now been exposed after upgrading to 1.21.19 because of the aforementioned change in 1.21.15 to not load bundles into memory.

    Obviously, doesn't help solve the issue of not being able to cancel DownloadDependenciesAsync.