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

Question Is it possible to pre-cache/serialize a reference to an IResourceLocation?

Discussion in 'Addressables' started by Lucas-Hehir, Oct 10, 2020.

  1. Lucas-Hehir

    Lucas-Hehir

    Joined:
    Jul 2, 2014
    Posts:
    41
    According to this page in the documentation, they mention that it may be possible to save performance by pre-loading IResourceLocation's and passing them in directly to Addressables rather than passing in a key. Being able to do so bypasses the key-based look-up each time you want to run some kind of operation on the asset in question apparently, which sounds awesome.

    Is that just talking about pre-loading at runtime, like building an index of IResourceLocations once early on? Or is it possible to pre-cache and serialize them within a scene? I understand that the new SerializeReference attribute lets us serialize references to things like interfaces... and a bit of preliminary code I tried out kind of works:

    Code (CSharp):
    1. public List<AssetReference> assRefs = new List<AssetReference>();
    2.  
    3. [SerializeReference]
    4. public List<IResourceLocation> locations = new List<IResourceLocation>();
    5.      
    6. void OnValidate()
    7. {
    8.     PrecacheResourceLocations();
    9. }
    10.  
    11. public async void PrecacheResourceLocations()
    12. {
    13.     locations.Clear();
    14.  
    15.     foreach (var assRef in assRefs)
    16.     {
    17.         var locationOp = Addressables.LoadResourceLocationsAsync(assRef.RuntimeKey);
    18.  
    19.         await locationOp.Task;
    20.                  
    21.         if (locationOp.Status != AsyncOperationStatus.Succeeded)
    22.         {
    23.             #if UNITY_EDITOR
    24.             Debug.LogError($"Failed to retrieve asset location for [{assRef.editorAsset.name}]");
    25.             #endif
    26.  
    27.             return;
    28.         }
    29.  
    30.         if (locationOp.Result == null || locationOp.Result.Count == 0)
    31.         {
    32.             #if UNITY_EDITOR
    33.             Debug.LogError($"Failed to retrieve asset location for [{assRef.editorAsset.name}]");
    34.             #endif
    35.  
    36.             return;
    37.         }
    38.  
    39.         locations.Add(locationOp.Result[0]);
    40.  
    41.         Addressables.Release(handle: locationOp);
    42.     }
    43. }
    If I log out the primary key for each location after validation, they're all there. But after closing and re-opening the scene, the locations list is the same length but each entry is empty. My understanding of proper serialization is pretty lacklustre so I can't tell if this is possible and I'm just going about it it wrong or if its not a thing because of the way IResourceLocations are gathered by
    LoadResourceLocationsAsync...