c# - Unity:如何将 AssetBundle 构建到每个 AssetBundle 的单独目录中
问题描述
我的项目包含多个 AssetBundle,我希望能够单击编辑器中的一个按钮,它将所有 AssetBundle 构建到它们自己适当命名的文件夹中。
换句话说,给定名为“A”和“B”的 AssetBundle,输出将是两个文件夹,其中“A”包含构建的捆绑资产,这些资产被标记为包含在“A”中,同样是“B”的类似文件夹.
我已经在一定程度上熟悉了AssetBundleBuild
和BuildPipeline
类。
这导致我制作了以下脚本(见下文)。
我觉得我已经很接近了,因为我已经获得了项目中所有资产包的列表,为它们设置了目录,并尝试构建它们。问题是我收到以下错误:
清单 AssetBundle 名称“example_bundle_name”与用户预定义的 AssetBundle 名称冲突。
请问我需要做什么才能完成这项工作?
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 时,它正在制作一个重复的资产包,其中包含具有相同名称的原始文件的所有内容。
解决方案
为什么要编写代码来构建 AssetsBundle?有什么 AssetBundle Browser 不能做的吗?只需下载Asset Store或Github并根据需要组织您的捆绑包。
推荐阅读
- javascript - 状态为空,在 useEffect 挂钩之后
- python - 是否可以将 .txt 的每一行存储为一个列表,然后再使用它?
- apache-flink - 为什么 RichFlatMapFunction 中不处理数据
- python - TypeError:TypeError 类型的对象不是 JSON 可序列化 Python
- http - Golang HTTP 自定义错误处理响应
- c# - 将上下文从控制器操作发送到 Blazor 页面
- zip - 如何使用 tar 和 pigz 单独压缩子文件夹?
- sql - 尝试implenet .env后的Django数据库问题
- regedit - 如何使用 CMD 脚本更改 regedit 的值?
- typescript - 如何修复我的 Stripe redirectToCheckout 集成错误?