记录一下unity图片统一压缩方案
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.Drawing;
using System.Reflection;
public class TextureDeal
{
#region atlas deal
public static float halveRate = 0.5f;
[MenuItem("Tools/CompressTexture")]
public static void CompressTexture()
{
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;
}
CompressTexture(dirPath);
}
}
private static void CompressTexture(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") || filePath.EndsWith(".tga") || filePath.EndsWith(".TGA")) && !filePath.Contains("Texture/UI") && !filePath.Contains("FBX/SM_badge_"))
{
//筛选出png和jpg图片
EditorUtility.DisplayProgressBar("处理中>>>", filePath, (float)i / (float)files.Length);
TextureImporter textureImporter = AssetImporter.GetAtPath(filePath) as TextureImporter;
if (textureImporter == null)
return;
textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
AssetDatabase.ImportAsset(filePath);
Texture2D texture = AssetDatabase.LoadAssetAtPath
TextureImporter textureImporter1 = AssetImporter.GetAtPath(filePath) as TextureImporter;
int width = 0;
int height = 0;
GetTextureOriginalSize(textureImporter, out width, out height);
int textureSize = Math.Max(height, width);
int defaultMaxTextureSize = textureSize;
defaultMaxTextureSize = (int)(defaultMaxTextureSize * halveRate);
int size = GetValidSize(defaultMaxTextureSize);
TextureImporterPlatformSettings settingAndroid = new TextureImporterPlatformSettings()
{
name = "Android",
overridden = true,
format = TextureImporterFormat.ASTC_5x5,
maxTextureSize = size,
};
textureImporter.SetPlatformTextureSettings(settingAndroid);
TextureImporterPlatformSettings settingIOS = new TextureImporterPlatformSettings()
{
name = "iOS",
overridden = true,
format = TextureImporterFormat.ASTC_5x5,
maxTextureSize = size,
};
textureImporter.SetPlatformTextureSettings(settingIOS);
textureImporter.SaveAndReimport();
DoAssetReimport(filePath, ImportAssetOptions.ForceUpdate|ImportAssetOptions.ForceSynchronousImport);
AssetDatabase.SaveAssets();
}
}
EditorUtility.ClearProgressBar();
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("成功", "处理完成!", "好的");
}
public static void GetTextureOriginalSize(TextureImporter ti, out int width, out int height)
{
if (ti == null)
{
width = 0;
height = 0;
return;
}
object[] args = new object[2] { 0, 0 };
MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(ti, args);
width = (int)args[0];
height = (int)args[1];
}
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
}