Unity:如何将 AssetBundle 构建到每个 AssetBundle 的单独目录中

Unity: How to build AssetBundles into separate directories per AssetBundle

我的项目包含多个 AssetBundle,我希望能够在编辑器中单击一个按钮,将所有 AssetBundle 构建到它们自己的适当命名的文件夹中

换句话说,给定名为 "A" 和 "B" 的 AssetBundle,输出将是两个文件夹,其中 "A" 包含标记为包含在 "A",以及 "B".

的类似文件夹

我已经在一定程度上熟悉了 AssetBundleBuildBuildPipeline 类。

这促使我制作了以下脚本(见下文)。

我觉得我已经很接近了,因为我得到了项目中所有资产包的列表,为它们设置了目录,并尝试构建它们。问题是我收到以下错误:

Manifest AssetBundle name "example_bundle_name" has conflict with the user predefined AssetBundle name.

请问我需要做什么才能完成这项工作?

public class BuildAssetBundles : MonoBehaviour
{
    private const string AssetBundleRootDirectory = "Assets/BuiltAssetBundles";
    private const BuildTarget BuildTarget = UnityEditor.BuildTarget.StandaloneWindows;

    [MenuItem("Build Asset Bundles/Build All Asset Bundles")]
    public static void BuildBundlesIntoDirectories()
    {
        Debug.Log("Asset bundle building started...");
        // Get all assets

        var assets = AssetDatabase.GetAllAssetPaths().ToArray();

        List<AssetBundleBuild> assetBundleBuilds = new List<AssetBundleBuild>();
        HashSet<string> processedBundles = new HashSet<string>();

        // Get asset bundle names from selection
        foreach (var o in assets)
        {
            var assetPath = o;
            var importer = AssetImporter.GetAtPath(assetPath);

            if (importer == null)
            {
                continue;
            }

            // Get asset bundle name & variant
            var assetBundleName = importer.assetBundleName;
            var assetBundleVariant = importer.assetBundleVariant;
            var assetBundleFullName = string.IsNullOrEmpty(assetBundleVariant) ? assetBundleName : assetBundleName + "." + assetBundleVariant;

            // Only process assetBundleFullName once. No need to add it again.
            if (processedBundles.Contains(assetBundleFullName))
            {
                continue;
            }

            processedBundles.Add(assetBundleFullName);

            AssetBundleBuild build = new AssetBundleBuild();

            build.assetBundleName = assetBundleName;
            build.assetBundleVariant = assetBundleVariant;
            build.assetNames = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleFullName);

            assetBundleBuilds.Add(build);
        }

        foreach (var assetBundle in assetBundleBuilds.ToArray())
        {
            if(!String.IsNullOrWhiteSpace(assetBundle.assetBundleName))
                BuildAnAssetBundle(assetBundle);
        }
        Debug.Log("Asset bundle building finished.");

    }

    static void BuildAnAssetBundle(AssetBundleBuild assetBundleToBuild)
    {
        // Put the bundles in a folder within the Assets directory.
        string assetBundleOutputDirectory = Path.Combine(AssetBundleRootDirectory, assetBundleToBuild.assetBundleName);
        if (string.IsNullOrWhiteSpace(assetBundleToBuild.assetBundleVariant))
        {
            Path.Combine(assetBundleOutputDirectory, assetBundleToBuild.assetBundleVariant);
        }

        if(!Directory.Exists(assetBundleOutputDirectory))
            Directory.CreateDirectory(assetBundleOutputDirectory);


        try
        {
            BuildPipeline.BuildAssetBundles(assetBundleOutputDirectory, new AssetBundleBuild[]{assetBundleToBuild}, BuildAssetBundleOptions.None, BuildTarget);
        }
        catch (Exception e)
        {
            Debug.Log(e);
        }
    }
}

非常感谢您的帮助和建议。

在我看来,这个错误似乎暗示我的方法有点不正确,也许在设置 AssetBundleBuild 时它正在制作一个重复的资产包,其中包含具有相同名称的原始资源的所有内容。

你为什么要编写代码来构建 AssetBundle? AssetBundle Browser 有什么不能做的吗? 只需下载它 Asset Store or Github 并根据需要组织您的捆绑包。

我已经解决了这个问题,方法是编写一个单独的方法来在构建 assetbundle 文件后对其进行组织。

这通过获取 assetbundle 文件的名称(减去扩展名)并创建一个同名目录(如果尚不存在)来实现。创建所有目录后,我将同名文件移动到相关目录中。

注意:在 Windows(至少),您将有两个文件,包本身和清单。清单有扩展名,但捆绑文件没有。我给文件一个临时文件扩展名,这样我就可以在移动后恢复之前,在无扩展包文件所在的同一目录中创建一个同名目录。