赞
踩
如题一个编辑器小工具,主要在于Editor接口的使用和判断一张图片有无alpha通道,下面直接上代码:
#region atlas deal
public static int CompressQuality = 50;
public static float halveRate = 0.5f;
[MenuItem("Tools/HalveAtlas")]
public static void HalveAtlas()
{
UnityEngine.Object[] objects = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets); //获取选择文件夹
for (int i = 0; i < objects.Length; i++)
{
string dirPath = AssetDatabase.GetAssetPath(objects[i]).Replace("\\", "/");
if (!Directory.Exists(dirPath))
{
EditorUtility.DisplayDialog("错误", "选择正确文件夹!", "好的");
continue;
}
HalveSprite(dirPath);
}
}
private static void HalveSprite(string dirPath)
{
string[] files = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
string filePath = files[i];
filePath = filePath.Replace("\\", "/");
if (filePath.EndsWith(".png") || filePath.EndsWith(".jpg"))
{
//筛选出png和jpg图片
EditorUtility.DisplayProgressBar("处理中>>>", filePath, (float)i / (float)files.Length);
TextureImporter textureImporter = AssetImporter.GetAtPath(filePath) as TextureImporter;
if (textureImporter == null)
return;
//判断图片有无alpha通道,有默认格式设置成:RGBA16;无默认格式设置成:RGB16
textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
AssetDatabase.ImportAsset(filePath);
Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(filePath);
int textureSize = Math.Max(texture.height, texture.width);
TextureImporterFormat defaultTextureFormat = TextureImporterFormat.RGB16;
if (texture.format == TextureFormat.RGB24)
{
//no alpha
defaultTextureFormat = TextureImporterFormat.RGB16;
}
else
{
defaultTextureFormat = TextureImporterFormat.RGBA16;
}
TextureImporterSettings settings = new TextureImporterSettings();
textureImporter.ReadTextureSettings(settings);
textureImporter.textureType = TextureImporterType.Advanced;
int defaultMaxTextureSize = settings.maxTextureSize;
defaultMaxTextureSize = Math.Min(textureSize, defaultMaxTextureSize);
defaultMaxTextureSize = (int)(defaultMaxTextureSize * halveRate);
settings.textureFormat = defaultTextureFormat;
settings.maxTextureSize = GetValidSize(defaultMaxTextureSize);
int androidMaxTextureSize = 0;
TextureImporterFormat androidTextureFormat = UnityEditor.TextureImporterFormat.ETC_RGB4;
bool isAndroidOverWrite = textureImporter.GetPlatformTextureSettings("Android", out androidMaxTextureSize, out androidTextureFormat);
if (true == isAndroidOverWrite)
{
androidMaxTextureSize = Math.Min(textureSize, androidMaxTextureSize);
androidMaxTextureSize = (int)(androidMaxTextureSize * halveRate);
textureImporter.SetPlatformTextureSettings("Android", GetValidSize(androidMaxTextureSize), androidTextureFormat, CompressQuality);
}
int iphoneMaxTextureSize = 0;
TextureImporterFormat iphoneTextureFormat = UnityEditor.TextureImporterFormat.PVRTC_RGBA4;
bool isIphoneOverWrite = textureImporter.GetPlatformTextureSettings("iPhone", out iphoneMaxTextureSize, out iphoneTextureFormat);
if (true == isIphoneOverWrite)
{
iphoneMaxTextureSize = Math.Min(textureSize, iphoneMaxTextureSize);
iphoneMaxTextureSize = (int)(iphoneMaxTextureSize * halveRate);
textureImporter.SetPlatformTextureSettings("iPhone", GetValidSize(iphoneMaxTextureSize), iphoneTextureFormat, CompressQuality);
}
textureImporter.SetTextureSettings(settings);
AssetDatabase.SaveAssets();
DoAssetReimport(filePath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
}
}
EditorUtility.ClearProgressBar();
EditorUtility.DisplayDialog("成功", "处理完成!", "好的");
}
private static int GetValidSize(int size)
{
int result = 0;
if (size <= 48)
{
result = 32;
}
else if (size <= 96)
{
result = 64;
}
else if (size <= 192)
{
result = 128;
}
else if (size <= 384)
{
result = 256;
}
else if (size <= 768)
{
result = 512;
}
else if (size <= 1536)
{
result = 1024;
}
else if (size <= 3072)
{
result = 2048;
}
return result;
}
public static void DoAssetReimport(string path, ImportAssetOptions options)
{
try
{
AssetDatabase.StartAssetEditing();
AssetDatabase.ImportAsset(path, options);
}
finally
{
AssetDatabase.StopAssetEditing();
}
}
#endregion
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。