Creating Addressable Asset from Editor Script

Hi,

Hopefully someone could help or point me in the right direction. I have currently got an Editor process where I download data from Google Sheets and then create multiple Scriptable Objects. Is there a way to add these to my default Addressable group when they are created?

Currently I am doing the following:

var file = ScriptableObject.CreateInstance<GameData>();
AssetDatabase.CreateAsset(file, path);
EditorUtility.SetDirty(file);
AssetDatabase.SaveAssets();

Thanks

Certainly :slight_smile:

public static AssetReference AddAssetToAddressables(UnityEngine.Object asset)
    {
        AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
        string assetPath = AssetDatabase.GetAssetPath(asset);
        string assetGUID = AssetDatabase.AssetPathToGUID(assetPath);
        return settings.CreateAssetReference(assetGUID);
    }

This piece of code should add your asset to the default group if the asset isn’t already addressable. Note: if the asset is already addressable, it will move it to the default group. Check out the other functions on the settings variable to see what else you can do. :slight_smile:

4 Likes

Nice one thank you

Thanks for this, just what I needed!

Do you know if it’s possible to change the Address of the newly created AssetReference? Like “Cool Asset” instead of its default path

Sure! You can do the following:

AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
string assetPath = AssetDatabase.GetAssetPath(asset);
string assetGUID = AssetDatabase.AssetPathToGUID(assetPath);
AddressableAssetEntry entry = settings.FindAssetEntry(assetGUID);
entry.address = "Cool Asset";
3 Likes

It works like a charm, thanks!