Search Unity

Move asset to addressable group from code

Discussion in 'Addressables' started by Hostur, Oct 16, 2018.

  1. Hostur

    Hostur

    Joined:
    Jul 6, 2015
    Posts:
    60
    Hi,
    I'm trying to assign assets to addressables from code. With asset bundles it was easy like:

    AssetImporter.GetAtPath(path).SetAssetBundleNameAndVariant(bundleName, "");

    How to move asset to addressable group and set addressable label-name on it?
    Is there any friendly api for that?
     
    JesOb likes this.
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    Assuming you have the settings object (or use
    AddressableAssetSettingsDefaultObject.Settings
    ), you can do
    settings.CreateGroup(...)
    and
    settings.CreateOrMoveEntry(...)
     
  3. RobRab2000

    RobRab2000

    Joined:
    Nov 28, 2012
    Posts:
    29
    Hi, I am currently converting our AssetBundle loading system over to Addressables and I have the same question too. For our current system I have a watch folder and any prefab that gets dropped inside gets assigned to an asset bundle and registered with the rest of our system. I would like to do the same with the Addressables system. I had a look into the CreateOrMoveEntry but the documentation is a little sparse. I was wondering if there was a good place to get started learning a bit more?
     
  4. wolilio

    wolilio

    Joined:
    Aug 19, 2019
    Posts:
    29
    use this to add/move file or folder to a group

    Code (CSharp):
    1.         public static void AddAssetToGroup(string path ,string groupName)
    2.         {
    3.             var group = AddressableAssetSettingsDefaultObject.Settings.FindGroup(groupName);
    4.             if (!group)
    5.             {
    6.                 throw new Exception($"Addressable : can't find group {groupName}");
    7.             }
    8.             var entry = AddressableAssetSettingsDefaultObject.Settings.CreateOrMoveEntry(AssetDatabase.AssetPathToGUID(path), group,
    9.                 false,
    10.                 true);
    11.  
    12.             if (entry == null)
    13.             {
    14.                 throw new Exception($"Addressable : can't add {path} to group {groupName}");
    15.             }
    16.         }