113 lines
4.0 KiB
C#
113 lines
4.0 KiB
C#
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEditor;
|
||
using System.Threading.Tasks;
|
||
using System.Linq;
|
||
|
||
|
||
namespace Logic.Editor
|
||
{
|
||
public class EditorWindow2D
|
||
{
|
||
public static List<string> MaxTextureSizeFolders = new List<string>
|
||
{
|
||
"ArtResources/TH1CharIllustrations",
|
||
"ArtResources/TH1UI/Main/UI_bg",
|
||
"ArtResources/TH1UI"
|
||
|
||
};
|
||
|
||
[MenuItem("Assets/批量设置图片压缩低精度", false, 20)]
|
||
private static void ConfigTextures()
|
||
{
|
||
string folderPath = AssetDatabase.GetAssetPath(Selection.activeObject);
|
||
string[] guids = AssetDatabase.FindAssets("t:Texture", new[] { folderPath });
|
||
|
||
// 创建进度条
|
||
int totalCount = guids.Length;
|
||
int processedCount = 0;
|
||
|
||
// 批量处理,每批50个
|
||
const int batchSize = 50;
|
||
var guidBatches = guids.Select((guid, index) => new { guid, index })
|
||
.GroupBy(x => x.index / batchSize)
|
||
.Select(g => g.Select(x => x.guid).ToList())
|
||
.ToList();
|
||
|
||
foreach (var batch in guidBatches)
|
||
{
|
||
EditorUtility.DisplayProgressBar("处理图片压缩", $"已处理 {processedCount}/{totalCount}", (float)processedCount / totalCount);
|
||
|
||
// 批量处理当前批次的图片
|
||
foreach (string guid in batch)
|
||
{
|
||
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
||
TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
|
||
|
||
if (textureImporter == null) continue;
|
||
|
||
// 设置图片导入器设置
|
||
if (IsMaxTexture(assetPath))
|
||
CompressTexturesMaxInFolder(textureImporter);
|
||
else
|
||
CompressTexturesMinInFolder(textureImporter);
|
||
|
||
processedCount++;
|
||
}
|
||
|
||
// 每批次统一导入
|
||
AssetDatabase.SaveAssets();
|
||
AssetDatabase.Refresh();
|
||
}
|
||
|
||
EditorUtility.ClearProgressBar();
|
||
EditorUtility.DisplayDialog("完成", $"已完成 {processedCount} 个图片的压缩设置!", "确定");
|
||
}
|
||
|
||
private static void CompressTexturesMinInFolder(TextureImporter importer)
|
||
{
|
||
if (!importer.isReadable)
|
||
{
|
||
var settings = new TextureImporterPlatformSettings
|
||
{
|
||
name = "Standalone",
|
||
overridden = true,
|
||
maxTextureSize = 512,
|
||
format = TextureImporterFormat.BC7,
|
||
compressionQuality = 100 // 使用100代表最佳质量
|
||
};
|
||
|
||
importer.SetPlatformTextureSettings(settings);
|
||
importer.SaveAndReimport();
|
||
}
|
||
}
|
||
|
||
private static void CompressTexturesMaxInFolder(TextureImporter importer)
|
||
{
|
||
if (!importer.isReadable)
|
||
{
|
||
var settings = new TextureImporterPlatformSettings
|
||
{
|
||
name = "Standalone",
|
||
overridden = true,
|
||
maxTextureSize = 2048,
|
||
format = TextureImporterFormat.BC7,
|
||
compressionQuality = 100 // 使用100代表最佳质量
|
||
};
|
||
|
||
importer.SetPlatformTextureSettings(settings);
|
||
importer.SaveAndReimport();
|
||
}
|
||
}
|
||
|
||
private static bool IsMaxTexture(string path)
|
||
{
|
||
if (string.IsNullOrEmpty(path)) return false;
|
||
foreach (var floder in MaxTextureSizeFolders)
|
||
{
|
||
if (path.Contains(floder)) return true;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
} |