Load files by name
By default, AssetBundles built with the Scriptable Build Pipeline (SBP) only support loading assets by full path. This is to avoid loading collisions that occur when two assets in the same AssetBundle have different full paths but the same file name.
To change this behavior, use the AssetBundleBuild.addressableNames field, or use the ContentPipeline.BuildAssetBundles API to change the loading path with IBundleBuildContent.Addresses.
The following example shows how to use the CompatibilityBuildPipeline methods to load a file by file name instead of by full file path. The ContentBuildInterface.GenerateAssetBundleBuilds method gets the set of bundles and assets to build, then modifies the addressableNames field to set the loading path of the file name instead of the full path.
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Build.Content;
using UnityEditor.Build.Pipeline;
public static class BuildAssetBundlesExample
{
public static bool BuildAssetBundles(string outputPath, bool forceRebuild, bool useChunkBasedCompression, BuildTarget buildTarget)
{
var options = BuildAssetBundleOptions.None;
if (useChunkBasedCompression)
options |= BuildAssetBundleOptions.ChunkBasedCompression;
if (forceRebuild)
options |= BuildAssetBundleOptions.ForceRebuildAssetBundle;
// Get the set of bundles to build
var bundles = ContentBuildInterface.GenerateAssetBundleBuilds();
// Update the addressableNames to load by the file name without extension
for (var i = 0; i < bundles.Length; i++)
bundles[i].addressableNames = bundles[i].assetNames.Select(Path.GetFileNameWithoutExtension).ToArray();
var manifest = CompatibilityBuildPipeline.BuildAssetBundles(outputPath, bundles, options, buildTarget);
return manifest != null;
}
}