using System.Collections.Generic; using UnityEditor.AddressableAssets.Settings; using UnityEditor.AddressableAssets.Settings.GroupSchemas; using UnityEditor.Build.Pipeline; using UnityEngine; using UnityEditor.Build.Content; using BuildCompression = UnityEngine.BuildCompression; using UnityEditor.Build.Player; namespace UnityEditor.AddressableAssets.Build.DataBuilders { /// /// Custom bundle parameter container that provides custom compression settings per bundle. /// public class AddressableAssetsBundleBuildParameters : BundleBuildParameters { internal const string k_AddressablesAddDefines = "ADDRESSABLES_ADD_DEFINES"; Dictionary m_bundleToAssetGroup; AddressableAssetSettings m_settings; string[] m_ExtraScriptingDefines; /// /// Create a AddressableAssetsBundleBuildParameters with data needed to determine the correct compression per bundle. /// /// The AddressableAssetSettings object to use for retrieving groups. /// Mapping of bundle identifier to guid of asset groups. /// The build target. This is used by the BundleBuildParameters base class. /// The build target group. This is used by the BundleBuildParameters base class. /// The path for the output folder. This is used by the BundleBuildParameters base class. public AddressableAssetsBundleBuildParameters(AddressableAssetSettings aaSettings, Dictionary bundleToAssetGroup, BuildTarget target, BuildTargetGroup group, string outputFolder) : base(target, group, outputFolder) { UseCache = true; ContiguousBundles = aaSettings.ContiguousBundles; #if NONRECURSIVE_DEPENDENCY_DATA NonRecursiveDependencies = aaSettings.NonRecursiveBuilding; #endif DisableVisibleSubAssetRepresentations = aaSettings.DisableVisibleSubAssetRepresentations; m_settings = aaSettings; m_bundleToAssetGroup = bundleToAssetGroup; //If default group has BundledAssetGroupSchema use the compression there otherwise check if the target is webgl or not and try set the compression accordingly if (m_settings.DefaultGroup.HasSchema()) BundleCompression = ConverBundleCompressiontToBuildCompression(m_settings.DefaultGroup.GetSchema().Compression); else BundleCompression = target == BuildTarget.WebGL ? BuildCompression.LZ4Runtime : BuildCompression.LZMA; if (aaSettings.StripUnityVersionFromBundleBuild) ContentBuildFlags |= ContentBuildFlags.StripUnityVersion; } /// /// Create a AddressableAssetsBundleBuildParameters with data needed to determine the correct compression per bundle. /// /// The AddressableAssetSettings object to use for retrieving groups. /// Mapping of bundle identifier to guid of asset groups. /// Builder input to pass in the target and group as well as extra scripting defines. /// The path for the output folder. This is used by the BundleBuildParameters base class. public AddressableAssetsBundleBuildParameters(AddressableAssetSettings aaSettings, Dictionary bundleToAssetGroup, AddressablesDataBuilderInput builderInput, string outputFolder) : this(aaSettings, bundleToAssetGroup, builderInput.Target, builderInput.TargetGroup, outputFolder) { // If ADDRESSABLES_ADD_DEFINES is in the passed-in extra scripting defines, apply development build and extra scripting defines bool addDefines = builderInput.ExtraScriptingDefines != null && System.Array.IndexOf(builderInput.ExtraScriptingDefines, k_AddressablesAddDefines) >= 0; if (!addDefines) return; if (builderInput.DevelopmentBuild) { ScriptOptions |= ScriptCompilationOptions.DevelopmentBuild; } if (builderInput.ExtraScriptingDefines != null) { m_ExtraScriptingDefines = StripEditorScriptingDefines(builderInput.ExtraScriptingDefines); } } private BuildCompression ConverBundleCompressiontToBuildCompression( BundledAssetGroupSchema.BundleCompressionMode compressionMode) { BuildCompression compresion = BuildCompression.LZMA; switch (compressionMode) { case BundledAssetGroupSchema.BundleCompressionMode.LZMA: break; case BundledAssetGroupSchema.BundleCompressionMode.LZ4: compresion = BuildCompression.LZ4; break; case BundledAssetGroupSchema.BundleCompressionMode.Uncompressed: compresion = BuildCompression.Uncompressed; break; } return compresion; } /// /// Get the compressions settings for the specified asset bundle. /// /// The identifier of the asset bundle. /// The compression setting for the asset group. If the group is not found, the default compression is used. public override BuildCompression GetCompressionForIdentifier(string identifier) { string groupGuid; if (m_bundleToAssetGroup.TryGetValue(identifier, out groupGuid)) { var group = m_settings.FindGroup(g => g != null && g.Guid == groupGuid); if (group != null) { var abSchema = group.GetSchema(); if (abSchema != null) return abSchema.GetBuildCompressionForBundle(identifier); else Debug.LogWarningFormat("Bundle group {0} does not have BundledAssetGroupSchema.", group.name); } else { Debug.LogWarningFormat("Unable to find group with guid {0}", groupGuid); } } return base.GetCompressionForIdentifier(identifier); } /// public override ScriptCompilationSettings GetScriptCompilationSettings() { return new ScriptCompilationSettings { group = Group, target = Target, options = ScriptOptions, extraScriptingDefines = m_ExtraScriptingDefines }; } /// /// Strips UNITY_EDITOR scripting defines from the provided array. /// These defines should not be passed to bundle builds since bundles are compiled without Editor code. /// /// The array of scripting defines to filter. /// A new array with UNITY_EDITOR defines removed, or null if the result would be empty. static string[] StripEditorScriptingDefines(string[] defines) { if (defines == null || defines.Length == 0) return defines; var filteredDefines = new List(); foreach (var define in defines) { if (!define.StartsWith("UNITY_EDITOR")) filteredDefines.Add(define); } return filteredDefines.Count > 0 ? filteredDefines.ToArray() : null; } } }