buildactions icon indicating copy to clipboard operation
buildactions copied to clipboard

Addressables do not work in build

Open AldeRoberge opened this issue 1 year ago • 1 comments

When building manually, everything works fine. But when I build using SuperUnityBuild, it seems like the addressables arent included in the build. I get a bunch of "missing element with given key" at runtime when doing Addressables.LoadAsync... Am I missing a step in the build config?

AldeRoberge avatar Jan 04 '24 19:01 AldeRoberge

You need to build addressables along with every build. I just made a script with it that you need to add to the Post-Build actions in the SUB window:

image

using SuperUnityBuild.BuildTool;

using System;

using UnityEditor;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.Build.Pipeline.Utilities;

using UnityEngine;

namespace SuperUnityBuild.BuildActions
{

    public class UploadAddressablePostBuild : BuildAction, IPostBuildPerPlatformAction
    {

        [Tooltip("True to upload the built addressables to your CCD. You must have this already set up.")]
        [SerializeField] private bool _uploadToCcd=true;

        public override void PerBuildExecute(BuildReleaseType releaseType, BuildPlatform platform, 
            BuildArchitecture architecture, BuildScriptingBackend scriptingBackend, 
            BuildDistribution distribution, DateTime buildTime, ref BuildOptions options, 
            string configKey, string buildPath)
        {
            var settings = AddressableAssetSettingsDefaultObject.Settings;

            // NOTE: you need to modify this to fit your addressable profile names
            string profileName = platform.platformName == "PC" ? "Windows" : platform.platformName;
            // set the active profile based on platform
            string profileId = settings.profileSettings.GetProfileId(profileName);
            settings.activeProfileId = profileId;
            Debug.Log($"Setting active profile with name '{profileName}' to ID: " + settings.activeProfileId);

            // addressables is currently bugged, so we have to clear the cache before building
            // otherwise it'll get stuck in a dependency loop
            var activeBuilder = settings.ActivePlayerDataBuilder;
            AddressableAssetSettings.CleanPlayerContent(activeBuilder);
            BuildCache.PurgeCache(false);
            Caching.ClearCache();

            // this will build your addressables based on your profile settings
            AddressableAssetSettings.BuildPlayerContent(out AddressablesPlayerBuildResult result);
            Debug.Log("Err: " + result.Error);

            // this will upload the addressables to your CCD if true
            if (_uploadToCcd)
            {
                var builderInput = new AddressablesDataBuilderInput(settings);
                var task = CcdBuildEvents.Instance.UploadAndRelease(builderInput, result);
                task.ContinueWith(t =>
                {
                    Debug.Log("Upload and release succeeded: " + t.Result);
                });
            }
        }

    }

}

celojevic avatar Jan 30 '24 16:01 celojevic

@celojevic can you provide script "CcdBuildEvents" as it is referenced in above script or any other generic way to upload stuff?

MadhurG-Hexaware avatar Mar 28 '24 07:03 MadhurG-Hexaware

@celojevic can you provide script "CcdBuildEvents" as it is referenced in above script or any other generic way to upload stuff?

That script is part of the unity CCD management package

celojevic avatar Mar 28 '24 15:03 celojevic