Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Adding addressable assets during play mode

Discussion in 'Addressables' started by Peter5897, Mar 19, 2020.

  1. Peter5897

    Peter5897

    Joined:
    Jan 4, 2017
    Posts:
    24
    Is it possible to add addressable assets that can be referenced while play mode is running? I have an external editor that acts as a file watcher and produces or updates unity content based on those changes. Those assets are added to an addressables group for our game to reference later. Is it possible to have the client access this new content without exiting play mode?

    The simplest example is that some file is added which generates a scriptable object which is then placed in to the addressables system. I've tried running something like:

    Code (CSharp):
    1. await Addressables.CheckForCatalogUpdates().Task;
    2. await Addressables.UpdateCatalogs().Task;
    3. await Addressables.InitializeAsync().Task;
    but the new assets don't seem to be picked up without restarting play mode.
     
  2. madGlory

    madGlory

    Joined:
    Jan 12, 2016
    Posts:
    44
    +1 we are also trying to get this to work but haven't found a way yet
     
  3. madGlory

    madGlory

    Joined:
    Jan 12, 2016
    Posts:
    44
    We were able to make this work by replacing the AddressableImpl object to a new instance using reflection.


    Code (CSharp):
    1. public class AddressablesHelpers {
    2.     [MenuItem("Tools/RefreshAddressables")]
    3.     public async static UniTaskVoid RefreshAddressables() {
    4.         await Addressables.InitializeAsync();
    5.         var assembly = Assembly.GetAssembly(typeof(Addressables));
    6.         var addressablesType = assembly.GetType("UnityEngine.AddressableAssets.Addressables");
    7.         var addressablesImplType = assembly.GetType($"UnityEngine.AddressableAssets.AddressablesImpl");
    8.         var newImpl = Activator.CreateInstance(
    9.             addressablesImplType,
    10.             new object[] { new LRUCacheAllocationStrategy(1000, 1000, 100, 10) })
    11.             ;
    12.         var privateStaticBindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
    13.         var m_addressablesInstance = addressablesType.GetField("m_AddressablesInstance", privateStaticBindingFlags).GetValue(null);
    14.         addressablesType.GetField("m_AddressablesInstance", privateStaticBindingFlags).SetValue(m_addressablesInstance, newImpl);
    15.     }
    16. }
     
    vitaliano_fanatee likes this.