直接看代码:文章来源地址https://www.toymoban.com/news/detail-503733.html
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class AssetBundleNameGenerate
{
[MenuItem("AssetBundles/generic ABName of assets", false, 100)]
public static void GenericAssetBundleNames()
{
GenericAssetBundleNamesInPath(Application.dataPath + "/prefabs", ".prefab", "prefab", SearchOption.AllDirectories, "", null);
}
/// <summary>
/// 对目录内指定后缀名的文件设置assetBundleName
/// </summary>
/// <param name="fullPath">文件路径</param>
/// <param name="fileExt">文件后缀</param>
/// <param name="typeName">类型名</param>
/// <param name="searchOption">搜索文件的类别</param>
/// <param name="suffixName">assetBundle后缀名</param>
/// <param name="filterStr">名字过滤(包含)</param>
private static void GenericAssetBundleNamesInPath(string fullPath, string fileExt, string typeName, SearchOption searchOption, string suffixName, string filterStr)
{
if (Directory.Exists(fullPath))
{
EditorUtility.DisplayProgressBar("设置AssetBundle名称", "正在设置AssetName名称中...", 0f);
var dir = new DirectoryInfo(fullPath);
var files = dir.GetFiles("*" + fileExt, searchOption);
for (int i = 0; i < files.Length; i++)
{
var fileInfo = files[i];
EditorUtility.DisplayProgressBar("设置AssetBundle名称", "正在设置AssetName名称中...", 1f * i / files.Length);
string assetBundleName = string.Empty;
if (string.IsNullOrEmpty(typeName) == false)
{
if (string.IsNullOrEmpty(filterStr) || fileInfo.Name.IndexOf(filterStr) >= 0)
{
string name = fileInfo.Name.Substring(0, fileInfo.Name.Length - fileExt.Length);
name = name.ToLower();
if (name.Contains(" "))
{
Debug.LogErrorFormat("资源名字存在空格符path = {0}", fileInfo.FullName);
}
assetBundleName = string.Format("{0}/{1}{2}.unity3d", typeName, name, suffixName);
}
}
var path = fileInfo.FullName.Substring(Application.dataPath.Length - "Assets".Length).Replace('\\', '/');
var importer = AssetImporter.GetAtPath(path);
if (importer != null && importer.assetBundleName != assetBundleName)
{
importer.assetBundleName = assetBundleName;
}
if (importer.assetBundleName != null && importer.assetBundleName != "")
{
importer.assetBundleVariant = null;
}
}
EditorUtility.ClearProgressBar();
}
else
{
Debug.LogErrorFormat("文件夹路径名出错:{0}", fullPath);
}
}
}
文章来源:https://www.toymoban.com/news/detail-503733.html
到了这里,关于Unity AssetsBundle一键为需要打包的预制体设置assetBundle名的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!