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;
}
}
Unity文件变更回调函数
最新推荐文章于 2023-10-24 22:27:27 发布