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;
namespace UnityEditor.AddressableAssets.Build.DataBuilders
{
///
/// Custom bundle parameter container that provides custom compression settings per bundle.
///
public class AddressableAssetsBundleBuildParameters : BundleBuildParameters
{
Dictionary m_bundleToAssetGroup;
AddressableAssetSettings m_settings;
///
/// 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;
}
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);
}
}
}