Unity 自动整理贴图生成AB包预览
下面展示一些 内联代码片
。
/// <summary>
/// 自动整理文件夹结构,并设置好AB包信息
/// </summary>
[MenuItem("Assets/2自动整理", false, 2)]
private static void AutoArrange()
{
string selectFolderPath = AssetDatabase.GetAssetPath(Selection.activeObject);
if (AssetDatabase.IsValidFolder(selectFolderPath) == false)
{
EditorUtility.DisplayDialog("警告", "请选中文件夹!", "确定");
return;
}
Object[] allAssets = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
// 检查是否已经存在Config文件
bool needHandleConfig = true;
foreach (var asset in allAssets)
{
string assetPath = AssetDatabase.GetAssetPath(asset);
string fullName = GetAssetFullNameByPath(assetPath);
if (fullName.Equals("Config.csv"))
{
int option = EditorUtility.DisplayDialogComplex("警告", "已经存在Config.csv配置文件,请选择执行方式。", "覆盖", "跳过", "取消");
switch (option)
{
case 0:
needHandleConfig = true;
break;
case 1:
needHandleConfig = false;
break;
case 2:
return;
}
}
}
// 检查是否已存在高清贴图包
foreach (var asset in allAssets)
{
string assetPath = AssetDatabase.GetAssetPath(asset);
if (AssetDatabase.IsValidFolder(assetPath))
{
string fullName = GetAssetFullNameByPath(assetPath);
if (fullName.Equals("Textures_HD", System.StringComparison.OrdinalIgnoreCase))
{
if (EditorUtility.DisplayDialog("警告", "可能已经存在高清贴图包: " + fullName + ",请仔细检查!", "覆盖", "取消"))
{
// 删除高清贴图文件夹后继续执行
AssetDatabase.DeleteAsset(assetPath);
}
else
{
// 取消
return;
}
}
}
}
EditorUtility.DisplayProgressBar("处理中", "请耐心等待", 0f);
allAssets = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
// 全部移动到根目录
foreach (var asset in allAssets)
{
string oldAssetPath = AssetDatabase.GetAssetPath(asset);
string newAssetPath = selectFolderPath + "/" + GetAssetFullNameByPath(oldAssetPath);
if (newAssetPath.Equals(selectFolderPath))
{
continue;
}
AssetDatabase.MoveAsset(oldAssetPath, newAssetPath);
}
// 删除空白文件夹
allAssets = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
foreach (var asset in allAssets)
{
string assetPath = AssetDatabase.GetAssetPath(asset);
if (assetPath.Equals(selectFolderPath))
{
continue;
}
if (AssetDatabase.IsValidFolder(assetPath))
{
AssetDatabase.DeleteAsset(assetPath);
}
}
if (needHandleConfig)
{
HandleConfig(selectFolderPath);
}
HandleMateirals(selectFolderPath);
HandleTextures(selectFolderPath);
HandleModels(selectFolderPath);
EditorUtility.ClearProgressBar();
}
/// <summary>
/// 处理材质球
/// </summary>
private static void HandleMateirals(string selectFolderPath)
{
string materialsFolder = selectFolderPath + "/Materials";
AssetDatabase.CreateFolder(selectFolderPath, "Materials");
Material[] materialAssets = Selection.GetFiltered<Material>(SelectionMode.DeepAssets);
foreach (Material materialAsset in materialAssets)
{
string oldMaterialAssetPath = AssetDatabase.GetAssetPath(materialAsset);
string newMaterialAssetPath = materialsFolder + "/" + GetAssetFullNameByPath(oldMaterialAssetPath);
AssetDatabase.MoveAsset(oldMaterialAssetPath, newMaterialAssetPath);
}
}
/// <summary>
/// 处理贴图,高清贴图自动加入ab包中
/// </summary>
private static void HandleTextures(string selectFolderPath)
{
string texturesFolder = selectFolderPath + "/Textures";
AssetDatabase.CreateFolder(selectFolderPath, "Textures");
string texturesHDFolder = selectFolderPath + "/Textures_HD";
AssetDatabase.CreateFolder(selectFolderPath, "Textures_HD");
string texturesHDAssetBundleName = texturesHDFolder.Remove(0, "Assets/".Length).ToLower();
// 复制或移动已经调整好大小的贴图到对应的文件夹中
Texture[] textureAssets = Selection.GetFiltered<Texture>(SelectionMode.DeepAssets);
for (int i = 0; i < textureAssets.Length; i++)
{
Texture textureAsset = textureAssets[i];
string oldTextureAssetPath = AssetDatabase.GetAssetPath(textureAsset);
string newTextureAssetPath = texturesFolder + "/" + GetAssetFullNameByPath(oldTextureAssetPath);
string newTextureHdAssetPath = texturesHDFolder + "/" + GetAssetFullNameByPath(oldTextureAssetPath);
ChangeTextureSize(textureAsset, HDTextureSize);
AssetDatabase.CopyAsset(oldTextureAssetPath, newTextureHdAssetPath);
ChangeTextureSize(textureAsset, LowTextureSize);
AssetDatabase.MoveAsset(oldTextureAssetPath, newTextureAssetPath);
AssetImporter assetImporter = AssetImporter.GetAtPath(newTextureHdAssetPath);
assetImporter.assetBundleName = texturesHDAssetBundleName;
assetImporter.SaveAndReimport();
EditorUtility.DisplayProgressBar("处理中", oldTextureAssetPath, (float)i / textureAssets.Length);
}
}
/// <summary>
/// 处理模型,预制体和动画
/// </summary>
private static void HandleModels(string selectFolderPath)
{
string modelsFolder = selectFolderPath + "/Models";
AssetDatabase.CreateFolder(selectFolderPath, "Models");
// 查找所有FBX名字信息
FileInfo[] files = new DirectoryInfo(selectFolderPath).GetFiles("*.FBX", SearchOption.AllDirectories);
List<string> fileNames = new List<string>();
for (int i = 0; i < files.Length; i++)
{
fileNames.Add(files[i].Name);
}
// 根据名字信息找到所有FBX模型
Object[] allAssets = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
List<Object> modelAssets = new List<Object>();
foreach (var asset in allAssets)
{
string assetPath = AssetDatabase.GetAssetPath(asset);
string modelName = GetAssetFullNameByPath(assetPath);
if (fileNames.Contains(modelName))
{
modelAssets.Add(asset);
}
}
// 仅移动和修改可读写
foreach (Object modelAsset in modelAssets)
{
string modelAssetPath = AssetDatabase.GetAssetPath(modelAsset);
string newModelAssetPath = modelsFolder + "/" + GetAssetFullNameByPath(modelAssetPath);
ModelImporter modelImporter = ModelImporter.GetAtPath(modelAssetPath) as ModelImporter;
modelImporter.isReadable = true;
modelImporter.SaveAndReimport();
AssetDatabase.MoveAsset(modelAssetPath, newModelAssetPath)
}
}
/// <summary>
/// 改变贴图大小
/// </summary>
private static void ChangeTextureSize(Texture texture, int maxTextureSize)
{
string texturePath = AssetDatabase.GetAssetPath(texture);
TextureImporter importer = AssetImporter.GetAtPath(texturePath) as TextureImporter;
TextureImporterSettings settings = new TextureImporterSettings();
importer.ReadTextureSettings(settings);
importer.maxTextureSize = maxTextureSize;
importer.SetTextureSettings(settings);
AssetDatabase.WriteImportSettingsIfDirty(texturePath);
AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);
}
/// <summary>
/// 处理配置文件
/// </summary>
private static void HandleConfig(string selectFolderPath)
{
string configFolder = selectFolderPath;
string configAssetPath = configFolder + "/Config.csv";
AssetDatabase.CopyAsset(CommonConfigAssetPath, configAssetPath);
}