Wednesday, January 10, 2024

Unity Addressables Content Catalog Refresh

 We're using Unity's Addressables to implement DLC for a multi-platform game, and on certain platforms, we don't actually know the path to the DLC until after it's loaded, which could be some time after the game has started.  One method is to use a multiple catalog system, like this one developed by LuGus-Jan and pdinklag.  In our case, though, we wanted to stick to a single catalog system if at all possible, and we were able to come up with a way to refresh the Addressables content catalog at runtime, though normally Unity does it once when the game starts up.  The code is written against Unity Addressables 1.19.19.

Refresh Addressables Content Catalog

The Addressables system allows you to specify code to dynamically construct load paths at runtime, but those paths are resolved to strings during the initial content catalog load, and take whatever value the code returns at that point.  Using this refresh operation, we can update the return value for the DLC path later while the game is running, and then force the Addressables system to re-resolve the paths to their correct values.  Addressables can then be accessed from the DLC as expected.

The public method for loading an Addressables content catalog is made assuming that data is being downloaded from the internet, and so includes an automatic caching feature to avoid redundant downloads.  However, the feature involves writing to Application.persistentDataPath, which is not allowed on some platforms.  For those cases, there is an alternate version that uses .NET reflection to access the internal methods and duplicate the same process, but avoid the step of reading and writing to the cache.  This may need to be revised with future versions of the Unity Addressables package.

No comments:

Post a Comment