Search Unity

Question Update local addressables while in playmode.

Discussion in 'Addressables' started by madGlory, Apr 12, 2021.

  1. madGlory

    madGlory

    Joined:
    Jan 12, 2016
    Posts:
    44
    We are working with addressables with respects to scriptable objects. These scriptable objects are being created and managed through code that automatically adds them to the addressables groups. The hope is that we can create new scriptable objects in playmode or in edit mode which could then be immediately referenced by a function that gets all of them.

    This is the code that adds them to the groups:

    Code (CSharp):
    1.         public async static UniTaskVoid OnWillCreateAsset(string assetPath) {
    2.             if (!assetPath.Contains(".meta")) {
    3.                 await UniTask.DelayFrame(1);
    4.                 var assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
    5.                 foreach (var asset in assets) {
    6.                     if (asset.GetType() == typeof(FooBlueprint)) {
    7.                         FooBlueprint foo = (FooBlueprint)asset;
    8.                         Debug.Log("Running creation function for " + foo.name);
    9.  
    10.                         var settings = AddressableAssetSettingsDefaultObject.Settings;
    11.                         var guid = AssetDatabase.AssetPathToGUID(assetPath);
    12.                         var group = settings.FindGroup("FooBlueprint");
    13.                         var entry = settings.CreateOrMoveEntry(guid, group);
    14.                         entry.labels.Add(Label);
    15.                         entry.address = $"Database/{Label}/{foo.name}.asset";
    16.                         settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entry, true);
    17.                         settings.SetDirty(AddressableAssetSettings.ModificationEvent.GroupMoved, group, true);
    18.                         AssetDatabase.Refresh();
    19.                         AssetDatabase.SaveAssets();
    20.  
    21.                     }
    22.                 }
    23.             }
    24.         }
    this works in that it adds them to the addressables groups in the addressables window but when we call this function to get all addressables of the type:

    Code (CSharp):
    1. async UniTask<List<FooBlueprint>> All() {
    2.         return (List<FooBlueprint>)await Addressables.LoadAssetsAsync<FooBlueprint>(Label, null);
    3.     }
    We find that it doesn't return the newly added scriptable objects, only updates upon entering/exiting playmode or after script compilation. We have tried the following calls to attempt to trigger an update but it doesn't seem to work.

    Code (CSharp):
    1.                         AssetDatabase.ImportAsset(assetPath);
    2.                         AssetDatabase.ImportAsset(settings.AssetPath);
    3.                         AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(group));
    4.                         await Addressables.CheckForCatalogUpdates();
    5.                         await Addressables.UpdateCatalogs();
    6.                         await Addressables.InitializeAsync();
    How can we update the addressables groups so that we can create new instances of the scriptable objects while in edit mode or in playmode and that the changes are always reflected when calling the loadallassets function?
     
  2. RoosterMai

    RoosterMai

    Joined:
    Nov 6, 2016
    Posts:
    8
    I also have the same problem, and the only way i found LoadAssetsAsync update content is after compile, so i switch to AssetDatabase from Addressables. if any one know how to refresh content when change in edit mode or unity have update this function, please tell me, thanks.