Search Unity

Cancel/Abort Addressable Download

Discussion in 'Addressables' started by Ferazel, Sep 4, 2019.

Thread Status:
Not open for further replies.
  1. Ferazel

    Ferazel

    Joined:
    Apr 18, 2010
    Posts:
    517
    I'm relatively new to addressables and trying to get a handle on them by getting a basic project setup that will have various remote asset bundles each with individual textures in them. The problem I'm running into is that the desired UI flow that seems to make the most sense doesn't seem to be allowed by the addressable API. The idea is that the user can download additional customization options for the game. Currently, I'm testing this out with different textures each in their own asset bundle.

    1) User is presented a list view of all of the options with the thumbnails being stored in the built-in assets (and potentially overridable with new content patches).
    2) The user selects one of the options. The user is presented a confirmation of "Do you want to download this asset of X bytes"?
    3) The user clicks OK and the box transitions to a progress bar with the download progress as well as a cancel button.
    4) After the download the message box hides while we load the texture from the downloaded bundle.

    #3 is where I am running into a problem, if the user is to cancel the download there doesn't appear to be an Abort API like there is an the UnityWebRequest? Am I missing a global API that maybe will handle this for AsyncOperations for Addressables? Is there another mechanic to handle canceling the download?

    Thanks!
     
  2. HugoClip

    HugoClip

    Joined:
    Feb 28, 2018
    Posts:
    52
    Afaik there is no method to cancel downloads. To support you will need to create your own Provider to track the UnityWebRequests. Basically replacing AssetBundleProvider.cs. I will probably need this feature in the future, so if you would like when I get there I don't mind sharing it with you.
     
  3. Ferazel

    Ferazel

    Joined:
    Apr 18, 2010
    Posts:
    517
    Thanks for the reply. It's disappointing that this use case was not implemented in the base addressable provider.
     
  4. foresightt

    foresightt

    Joined:
    Jan 3, 2020
    Posts:
    3
    Hey @HugoClip Can you please share your own provider. It will help a lot.
    I want the same workflow as describe in above post.
     
    better_walk_away and aka3eka like this.
  5. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    Had anyone tried to access UnityWebRequest from inside of a AsyncOpertaionHandle using reflexion to cancel the download?
     
  6. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    Hey, @unity_bill, could you please look at this request?
    Is there any way to abort a loading request being processed by the Addressables system?
     
    phobos2077 likes this.
  7. araki_yuta

    araki_yuta

    Joined:
    Jan 21, 2020
    Posts:
    14
    Copy
    UnityEngine.ResourceManagement.WebRequestQueue
    to somewhere in your namespace

    add something like
    Code (CSharp):
    1.         public static void AbortRequest()
    2.         {
    3.             foreach (var req in s_ActiveRequests)
    4.             {
    5.                 req.webRequest.Abort();
    6.             }
    7.  
    8.             foreach (var req in s_QueuedOperations)
    9.             {
    10.                 req.webRequest.Abort();
    11.             }
    12.         }
    13.  
    and make the custom WebRequestQueue not internal.
    Now you should be able to call it when you need to stop it.

    You need to customize AssetBundleProvider also to use customized version of WebRequestQueue.
     
    funkyCoty, phobos2077 and aka3eka like this.
  8. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32

    Thanks for the code and the whole idea!

    I have made a copy of WebRequestQueue.cs and renamed the class to WebRequestQueueAbortable. Added the method you suggested. By the way, there is no webRequest field in items of s_QueuedOperations. So there should be "req.m_WebRequest.Abort();" instead of "req.webRequest.Abort();" - that's because s_QueuedOperations contains a queue of WebRequestQueueOperation objects (not UnityWebRequest objects).

    I also copied AssetBundleProvider.cs, renamed the class to AssetBundleProviderAbortable and replaced WebRequestQueue with WebRequestQueueAbortable.

    My custom provider has appeared in the list of available providers in Unity Editor in Group settings. It's being called and works fine. And the call of UnityEngine.ResourceManagement.WebRequestQueueAbortable.Abort(); does the magic.

    But there are 2 problems:

    1. The solution aborts all active and queued downloads. It's quite easy to change Abort() method to cancel only specific download (tracking of m_WebRequestQueueOperation in AssetBundleProviderAbortable does the magic). But the problem is to access the instance of my AssetBundleProviderAbortable associated with specific resource load request.

    2. After calling Abort() Addressables shows a few exceptions in console (that's ok, it's a typical behavior) but the problem is that AsyncOperationHandle.Complete handler is not called after the abortion. I believe this happens because at some point in Addressables the exception is not being properly handled.

    Any ideas on these?
     
  9. araki_yuta

    araki_yuta

    Joined:
    Jan 21, 2020
    Posts:
    14
    Cool to hear that it's working for you.

    For problem1, may be you can change the queue to dictionary and make WebRequest accessible by key or something, so you could have Abort(string key = null). Although it may be hard to make relation from generic address to per bundle id, because you are working with "AssetBundle" on web request level but point of Addressables is rather hide all that detail.

    For problem2, I'd say you can't really help it. This is rather hacky way to get things done. Ideally AsyncOperationHandler should provide something like cancellation token for Async/Await mechanism in C#, but that's not provided AFAK. I figure its rather difficult to make this properly handled throughout whole Addressables chain because for Addressables, Async/Await is optional feature (like unity have to support coroutine and all?).

    BTW, I'm not sure if max queue size 500 is sensible default value in WebRequestQueue.
     
    aka3eka likes this.
  10. aka3eka

    aka3eka

    Joined:
    May 10, 2019
    Posts:
    32
    Thanks for your help.
    It seems solving the problem1 more or less "clean" requires to make some changes in Addressables itself which will make problems later when updating the package.

    The problem2 could be solved by manually calling completion handlers in Addressables wrapper code.

    I hope Unity Technologies will add cancellation method in future releases because it's vital for mobile apps.
     
  11. araki_yuta

    araki_yuta

    Joined:
    Jan 21, 2020
    Posts:
    14
    Hmm interesting, I'll look into it! Thanks for sharing :)
     
    aka3eka likes this.
  12. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    Cancelling methods is still not added to the Addressables API. This feature is urgently needed, quite often we will have to cancel download operation so that the game wouldn't explode when we have hundreds of bundles that could be downloaded at the same time.
     
  13. CameronND

    CameronND

    Joined:
    Oct 2, 2018
    Posts:
    89
    +1 to adding abort functionality.
     
  14. therobby3

    therobby3

    Joined:
    Jan 30, 2019
    Posts:
    131
    +1

    Going to add to this. I need to be able to cancel loading of an addressable as well. I'm surprised there isn't a way to do this already. For now, I'm probably going to have to implement some rather sloppy alternatives.

    Going to subscribe to this thread in hopes that it gets added in.
     
    phobos2077 and ProtoTerminator like this.
  15. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    It seems the default implementation of AssetBundleProvider is missing some basic functionality. I'm going to have to replace it with a custom copy (including custom WebRequestQueue) so I can have:

    - Real download progress in bytes
    - Download abort functionality

    The good thing is we have the option to replace asset/bundle providers. The bad thing is we have to copy large chunks of Addressables code. These classes should have less internal stuff and be written in a more flexible/extensible manner.

    Something to consider for Addressables 2.0 @unity_bill ?
     
    FrostFT, tealm, Prodigga and 3 others like this.
  16. pontos_developer

    pontos_developer

    Joined:
    May 20, 2020
    Posts:
    15
    Hi,@aka3eka I meet same problem when I aborted Addressable.
    How did you fixed problem 2?
    After I called AbortRequest(), and I want to LoadAssetAsync<GameObject>(key) again , it run out
    "Attempting to use an invalid operation handle"
    How can I calling completion?
    I tried to find Initialize AsyncOperationHandler or Refresh it, but there is no use.
    Did you meet same problem right ?
    Thank you .
     
    Last edited: Sep 24, 2020
  17. AriyaSD

    AriyaSD

    Joined:
    Nov 2, 2019
    Posts:
    24
    Use Addressables.LoadAssetAsync() instead of AssetReference.LoadAssetAsync().
     
    aka3eka likes this.
  18. felipe-shida-playkids

    felipe-shida-playkids

    Joined:
    Sep 16, 2020
    Posts:
    13
    +1 on abort functionality to addressable web requests.

    I'm having some "Attempting to use an invalid operation handle" where an op handle finishes inline before the BundledAssetProvider expected it to.

    Any news about it @unity_bill?
     
  19. FloBeber

    FloBeber

    Joined:
    Jun 9, 2015
    Posts:
    166
    +1

    Still now way to cancel a download operation in Addressable 1.16?
     
    aka3eka likes this.
  20. JanSenseVR

    JanSenseVR

    Joined:
    Aug 31, 2018
    Posts:
    5
    Any updates on this? Abort on any roadmap that is accessable? Addressables without abort is still useful but it critically cripples the functionality of any mobile app that loads more than a few bundles at start (which is what the Addressables system was built for right?). In total, we can have up to a few gigabytes of data loaded from remote servers and being able to cancel downloads if the user decides not to view the content is critical.
     
  21. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    Don't wait, just write custom AssetBundleProvider. Should be an acceptable solution until they add it properly.
     
  22. jacky-kschou

    jacky-kschou

    Joined:
    May 4, 2013
    Posts:
    73
    +1, we need this feature
     
  23. megavoid-de

    megavoid-de

    Joined:
    Sep 29, 2020
    Posts:
    8
  24. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
  25. Raghavendra

    Raghavendra

    Joined:
    Mar 14, 2014
    Posts:
    52
  26. phobos2077

    phobos2077

    Joined:
    Feb 10, 2018
    Posts:
    350
    I still think using the "default" asset system is better, despite all of it's flaws (you can work around all of them anyway). Such an important piece of project architecture and using a third-party plugin made by 1 random person is not a good idea generally.. There are exceptions though, like excellent UniRx library. But I would argue that it's a bit different - more focused on specific algorithmic tasks, easily Unit-testable for regressions etc. Also the person behind it is not some random...

    Another argument for Addressables is just how closely it is integrated with SPB+Unity itself. Often some problems in Addressables workflow end up being fixed in C++ part of Unity, only UT can do something like this. And this will be the way it is as long as AssetBundles are built and loaded by C++ code.
     
  27. Raghavendra

    Raghavendra

    Joined:
    Mar 14, 2014
    Posts:
    52
    I referenced that post just to point the issue the author was referring to if we cancel a download and how he solved it. Maybe I should have mentioned it in my previous post. :oops:
     
    phobos2077 likes this.
Thread Status:
Not open for further replies.