public class AssetImportTest : AssetPostprocessor
{
// 包名裁切的前缀
static string cutPrefix = "Assets/Resources/";
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
// 为资源设置包名
SetupAssetBundleName(movedAssets)
}
/// <summary>
/// 设置对象的包名
/// </summary>
/// <param name="importer">导入对象AssetImporter</param>
static void SetupAssetBundleName(AssetImporter importer)
{
if(Directory.Exists(importer.assetPath))
{
// 目标对象为文件夹
return;
}
string assetDir = Path.GetDirectoryName(importer.assetPath).FormatPath();
// 检查是否是Resources,非Resource资源不设置包名
if(assetDir.IndexOf(cutPrefix) < 0)
{
// 非目标前缀内的资源
return;
}
//
string abName = assetDir.Replace(cutPrefix, string.Empty);
abName = abName.FormatToAB();
if(abName.Equals(importer.assetBundleName))
{
// 重新导入等情况,包名没变
return;
}
importer.assetBundleName = abName;
}
}
01-11
1663
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-30
2249
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-09