资源:Assets/GameContent/Scenes/ui_login.unity
AssetBundle name自动设置为: scenes/ui_login
using UnityEditor;
public class AssetsImportMgr : AssetPostprocessor
{
static void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths)
{
autoSetAssetBundleName(importedAssets);
}
static string[] ResSuffix = { ".unity", ".prefab" };
static string RootDir = "GameContent";
/// <summary>
/// 资源导入时自动设置AssetBundle name
/// </summary>
/// <param name="importedAssets"></param>
static void autoSetAssetBundleName(string[] importedAssets)
{
foreach (var path in importedAssets)
{
//是否GameContent目录下
int firstIndex = path.IndexOf(RootDir) + RootDir.Length + 1;
if(firstIndex == -1)
continue;
//后缀是否对
int secondIndex = -1;
string pathLower = path.ToLower();
for (int i = 0; i < ResSuffix.Length; i++)
{
if (pathLower.EndsWith(ResSuffix[i]))
{
secondIndex = pathLower.LastIndexOf(ResSuffix[i]);
break;
}
}
if (secondIndex == -1)
continue;
string bundleName = string.Empty;
if (secondIndex >= 0)
bundleName = pathLower.Substring(firstIndex, secondIndex - firstIndex);
//设置name
AssetImporter importer = AssetImporter.GetAtPath(path);
if(importer != null)
{
importer.assetBundleName = bundleName;
importer.assetBundleVariant = "";
}
}
}
}