Search Unity

How to fetch latest catalog and re-initialize Addressables in runtime?

Discussion in 'Addressables' started by Kichang-Kim, Aug 7, 2018.

  1. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    275
    Hi. Is there any API for force refreshing the catalog to retreive latest assets from remote server?

    Here is detail:

    In mobile environment, users somtimes leave their device for a long time while game app is runnig on background. So when they re-activate game app, its local catalog cache may be different to server's latest asset on CDN. In this case, game app needs to detect asset catalog change and re-download catalog and assets.

    Also if above API is available, what happened when the asset which is loaded to memory is updateted on server? should I manually release all assets and re-load it manually?
     
  2. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    829
    In theory you could look through our initialization code and cobble together something that would do this, but I wouldn't recommend it for anyone other than very advanced users. Dealing with all the ref-counting, loaded or not loaded assets, and loaded or not loaded catalogs gets fairly complicated.
    We are aware that this situation happens, but haven't spent much time on it yet. Odds are the general use case will have to involve relaunching the app, but that has more to do with how the user manages their data.

    -Bill
     
  3. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    275
    @unity_bill
    Thanks for reply. I think Addressables needs two API at least for production environment, because this situation quite happens in the mobile game.

    1. Catalog hash check : Check whether local catalog hash and remote catalog hash are same. Using this API, app can show dialog like "Game data is changed, You need to re-launch app.". or if restart API is available (see below), "Game data is updated, app will be restarted automatically." by using that API and changing current scene to first scene.

    2. Restart Addressables, including unload all loaded assetbundles and clear all reference count to zero. (also clear all internal object pools) : This needs because Unity does not provide app re-launching feature.

    With these features, I can move my in-house asset management system to Addressables :)
     
  4. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    275
    @unity_bill
    Hi, is there any information about this features? I checked latest (0.3.5) Addressables's source code then found that this features can be implemented by adding force-refreshing flag to Addressables.Initialize().

    From
    Code (CSharp):
    1. public static IAsyncOperation<IResourceLocator> Initialize()
    2. {
    3.     if (s_initializationOperation != null)
    4.         return s_initializationOperation;
    5.  
    6.     ...
    7. }
    To
    Code (CSharp):
    1. public static IAsyncOperation<IResourceLocator> Initialize(bool forceRefresh = false)
    2. {
    3.     if (s_initializationOperation != null && !forceRefresh)
    4.         return s_initializationOperation;
    5.  
    6.     ...
    7. }
    Of course, other event-registrations should be carefully handled, like

    SceneManager.sceneUnloaded += OnSceneUnloaded;


    By allowing this API, developer can handle runtime-changed assets without restarting application. Dealing with ref-count, loading assets and internal running operations is doesn't matter because the purpose of this method is re-initialization, so simply destroying all internal state (ref count, assets, catalogue and operations ...) is enough.

    Also, catalog hash check can be implemented by comparing remote-hash (catalog_XXXX.hash on server) and local hash. Although this can be implemented by developer, officailly supported API needed because that internal Addressables's implementations can be changed in future.

    Thanks.
     
  5. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    829
    No update on this yet. You are correct that the `forceRefresh` flag could trigger a new initialization, but just doing that will cause all sorts of issues if the game has already started loading things. This is not a straightforward feature to create, but I agree that it is useful, and is on our list.
     
  6. AceEX7

    AceEX7

    Joined:
    Feb 2, 2014
    Posts:
    10
    @unity_bill any updates about fetching the latest catalog at runtime?
     
  7. yoshitaka-arishima

    yoshitaka-arishima

    Joined:
    Jul 8, 2019
    Posts:
    3
    Our mobile game project also needs these APIs. Without these APIs, we cannot use Addressables.
    We want you to implement these APIs as soon as possible.
     
  8. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    829
    Number one should be fairly straightforward for you to add yourself. You can just trigger a download of the remote hash.

    Number two is a bit tougher. Still doable in your own code, but this is the larger task we are looking at ways to solve ourselves.

    In the interim, I definitely encourage you to tweak addressables to meet your needs. This tool will often not solve all problems for a large game without some small tweaks. For large production games, addressables will often be a framework that devs can build customization on top of.