Search Unity

Set asset as addressable through script?

Discussion in 'Addressables' started by hivemindhermit, Jul 30, 2019.

  1. hivemindhermit

    hivemindhermit

    Joined:
    Dec 13, 2016
    Posts:
    9
    What title says. I tried searching the forum because it feels like this should've been addressed (haw haw, see what I did there) already, but apparently I'm crap at it... So. Can you set an asset as addressable (and maybe automatically simplify the address) through an editor script?
     
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    464
  3. tom_mardis

    tom_mardis

    Joined:
    Mar 11, 2020
    Posts:
    10
    In case anyone else gets here from google, the only example on the net that I've been able to find for setting an asset as addressable is Favo-Yang's project. It's a neat project but it took me a while to untangle what I needed to just simply create/remove groups and labels, then set an object as addressable.

    Also, when you run CreateOrMoveEntry(), if your labels and group names aren't correct you won't get an error it will just silently fail, so watch out for that.

    Hope this helps someone else :)


    //Use this object to manipulate addressables
    var settings = AddressableAssetSettingsDefaultObject.Settings;

    Create and Remove groups and labels and custom address:
    string group_name = "YourGroupName";
    string label_name = "YourLabelName";
    string path_to_object = "Assets/path/to/your/object/";
    string custom_address = "Custom address here";

    //Create a group with no schemas
    //settings.CreateGroup(group_name, false, false, false, new List<AddressableAssetGroupSchema> { settings.DefaultGroup.Schemas[0] });
    //Create a group with the default schemas
    settings.CreateGroup(sim_name, false, false, false, settings.DefaultGroup.Schemas);

    //Create a Label
    settings.AddLabel(label_name, false);

    //Remove a group
    AddressableAssetGroup g = settings.FindGroup(group_name);
    settings.RemoveGroup(g);
    //Remove a label
    settings.RemoveLabel(label_name, false);

    //Make a gameobject an addressable
    AddressableAssetGroup g = settings.FindGroup(group_name);
    var guid = AssetDatabase.AssetPathToGUID(path_to_object);
    //This is the function that actually makes the object addressable
    var entry = settings.CreateOrMoveEntry(guid, g);
    entry.labels.Add(label_name);
    entry.address = custom_address;
    //You'll need these to run to save the changes!
    settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entry, true);
    AssetDatabase.SaveAssets();
     
    Last edited: Dec 9, 2020
    antonsem, MagiJedi, gooby429 and 23 others like this.
  4. jeremedia

    jeremedia

    Joined:
    Apr 21, 2015
    Posts:
    63
    Many thanks! Why this exact info isn't in the manual is darn mystery.
     
    MostHated likes this.
  5. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
  6. tom_mardis

    tom_mardis

    Joined:
    Mar 11, 2020
    Posts:
    10
    MostHated likes this.