Search Unity

Get key from AssetReference

Discussion in 'Addressables' started by Ziboo, Apr 28, 2020.

  1. Ziboo

    Ziboo

    Joined:
    Aug 30, 2011
    Posts:
    356
    Hello,

    I'm currently pre loading and instantiating all my assets at the begin of my game, and I store them in a Dictionary.

    Code (CSharp):
    1.         private static void OnFactoryLoadedCompleted(AsyncOperationHandle<IList<IResourceLocation>> obj)
    2.         {
    3.             if (obj.Status == AsyncOperationStatus.Succeeded)
    4.             {
    5.                 if (obj.Result != null)
    6.                 {
    7.                     foreach (var result in obj.Result)
    8.                     {
    9.                         Addressables.LoadAssetAsync<Object>(result).Completed += o =>
    10.                         {
    11.                             store.Add(result.PrimaryKey, o.Result);
    12.                         };
    13.                     }
    14.                 }
    15.                 isLoaded = true;
    16.             }
    17.         }
    I have AssetReferences on ScriptableObject that I want to instantiate.
    But AssetReference doens't hold the key to the Object, only the GUID.
    I need to use the key because I'm saving my game entities based on the key.
    I don't want to use the GUID to do so if I want to replace an object later with another one I can't force unity to regenerate the same GUID (or at least it will be more complicated)

    Any solution ?

    Thanks
     
    Last edited: Apr 28, 2020