docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Start a build from a script

    To start a build from another script, call the AddressableAssetSettings.BuildPlayerContent method.

    Before starting the build, set the active Profile and the active build script. You can also set a different AddressableAssetSettings object than the default.

    BuildPlayerContent takes into consideration the following information when performing the build:

    • AddressableAssetSettingsDefaultObject
    • ActivePlayerDataBuilder
    • The addressables_content_state.bin file.

    Set the AddressableAssetSettings

    The settings defined by AddressableAssetSettings include the list of groups and the profile to use.

    To access the settings displayed in the Unity Editor (menu: Window > Asset Management > Addressables > Settings), use the static AddressableAssetSettingsDefaultObject.Settings property. However, if desired, you can use a different settings object for a build.

    To load a custom settings object in a build:

    
    static void getSettingsObject(string settingsAsset)
    {
        // This step is optional, you can also use the default settings:
        //settings = AddressableAssetSettingsDefaultObject.Settings;
    
        settings
            = AssetDatabase.LoadAssetAtPath<ScriptableObject>(settingsAsset)
                as AddressableAssetSettings;
    
        if (settings == null)
            Debug.LogError($"{settingsAsset} couldn't be found or isn't " +
                           $"a settings object.");
    }
    
    

    Set the active profile

    A build started with BuildContent uses the variable settings of the active profile. To set the active profile as part of your customized build script, assign the ID of the desired profile to the activeProfileId field of the AddressableAssetSettingsDefaultObject.Settings object.

    The AddressableAssetSettings object contains the list of profiles. Use the name of the desired profile to look up its ID value and then assign the ID to the activeProfileId variable:

    
    static void setProfile(string profile)
    {
        string profileId = settings.profileSettings.GetProfileId(profile);
        if (String.IsNullOrEmpty(profileId))
            Debug.LogWarning($"Couldn't find a profile named, {profile}, " +
                             $"using current profile instead.");
        else
            settings.activeProfileId = profileId;
    }
    
    

    Set the active build script

    The BuildContent method launches the build based on the current ActivePlayerDataBuilder setting. To use a specific build script, assign the index of the IDataBuilder object in the AddressableAssetSetting.DataBuilders list to the ActivePlayerDataBuilderIndex property.

    The build script must be a ScriptableObject that implements IDataBuilder and you must add it to the DataBuilders list in the AddressableAssetSettings instance. Once added to the list, use the standard List.IndexOf method to get the index of the object.

    
    static void setBuilder(IDataBuilder builder)
    {
        int index = settings.DataBuilders.IndexOf((ScriptableObject)builder);
    
        if (index > 0)
            settings.ActivePlayerDataBuilderIndex = index;
        else
            Debug.LogWarning($"{builder} must be added to the " +
                             $"DataBuilders list before it can be made " +
                             $"active. Using last run builder instead.");
    }
    
    

    Launch a build

    After setting the profile and builder to use, you can launch the build:

    
    static bool buildAddressableContent()
    {
        AddressableAssetSettings
            .BuildPlayerContent(out AddressablesPlayerBuildResult result);
        bool success = string.IsNullOrEmpty(result.Error);
    
        if (!success)
        {
            Debug.LogError("Addressables build error encountered: " + result.Error);
        }
    
        return success;
    }
    
    

    To check for success, use BuildPlayerContent(out AddressablesPlayerBuildResult result). result.Error contains any error message returned if the Addressables build failed. If string.IsNullOrEmpty(result.Error) is true, the build was successful.

    Example script to launch build

    The following example adds two menu commands to the Window > Asset Management > Addressables menu in the Editor. The first command builds the Addressable content using the preset profile and build script. The second command builds the Addressable content, and if it succeeds, builds the Player.

    
    #if UNITY_EDITOR
        using UnityEditor;
        using UnityEditor.AddressableAssets.Build;
        using UnityEditor.AddressableAssets.Settings;
        using System;
        using UnityEngine;
    
        internal class BuildLauncher
        {
            public static string build_script
                = "Assets/AddressableAssetsData/DataBuilders/BuildScriptPackedMode.asset";
    
            public static string settings_asset
                = "Assets/AddressableAssetsData/AddressableAssetSettings.asset";
    
            public static string profile_name = "Default";
            private static AddressableAssetSettings settings;
    
    
            static void getSettingsObject(string settingsAsset)
            {
                // This step is optional, you can also use the default settings:
                //settings = AddressableAssetSettingsDefaultObject.Settings;
    
                settings
                    = AssetDatabase.LoadAssetAtPath<ScriptableObject>(settingsAsset)
                        as AddressableAssetSettings;
    
                if (settings == null)
                    Debug.LogError($"{settingsAsset} couldn't be found or isn't " +
                                   $"a settings object.");
            }
    
    
    
            static void setProfile(string profile)
            {
                string profileId = settings.profileSettings.GetProfileId(profile);
                if (String.IsNullOrEmpty(profileId))
                    Debug.LogWarning($"Couldn't find a profile named, {profile}, " +
                                     $"using current profile instead.");
                else
                    settings.activeProfileId = profileId;
            }
    
    
    
            static void setBuilder(IDataBuilder builder)
            {
                int index = settings.DataBuilders.IndexOf((ScriptableObject)builder);
    
                if (index > 0)
                    settings.ActivePlayerDataBuilderIndex = index;
                else
                    Debug.LogWarning($"{builder} must be added to the " +
                                     $"DataBuilders list before it can be made " +
                                     $"active. Using last run builder instead.");
            }
    
    
    
            static bool buildAddressableContent()
            {
                AddressableAssetSettings
                    .BuildPlayerContent(out AddressablesPlayerBuildResult result);
                bool success = string.IsNullOrEmpty(result.Error);
    
                if (!success)
                {
                    Debug.LogError("Addressables build error encountered: " + result.Error);
                }
    
                return success;
            }
    
    
            [MenuItem("Window/Asset Management/Addressables/Build Addressables only")]
            public static bool BuildAddressables()
            {
                getSettingsObject(settings_asset);
                setProfile(profile_name);
                IDataBuilder builderScript
                    = AssetDatabase.LoadAssetAtPath<ScriptableObject>(build_script) as IDataBuilder;
    
                if (builderScript == null)
                {
                    Debug.LogError(build_script + " couldn't be found or isn't a build script.");
                    return false;
                }
    
                setBuilder(builderScript);
    
                return buildAddressableContent();
            }
    
            [MenuItem("Window/Asset Management/Addressables/Build Addressables and Player")]
            public static void BuildAddressablesAndPlayer()
            {
                bool contentBuildSucceeded = BuildAddressables();
    
                if (contentBuildSucceeded)
                {
                    var options = new BuildPlayerOptions();
                    BuildPlayerOptions playerSettings
                        = BuildPlayerWindow.DefaultBuildMethods.GetBuildPlayerOptions(options);
    
                    BuildPipeline.BuildPlayer(playerSettings);
                }
            }
        }
    #endif
    
    

    If your build script makes setting changes that require a domain reload, you should run the build script using Unity command line options, instead of running it interactively in the Editor. Refer to Handle domain reloads for more information.

    Additional resources

    • AddressableAssetSettings.BuildPlayerContent API reference
    • Handle domain reloads
    • AddressableAssetSettings API reference
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)