1. 程式人生 > >Unity圖集處理小工具:批量設定圖集的MaxSize及壓縮格式

Unity圖集處理小工具:批量設定圖集的MaxSize及壓縮格式

如題一個編輯器小工具,主要在於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