Unity之图片压缩,批量设置,导入自动设置

代码如下:

批量设置:

  1 using System.Collections.Generic;
  2 using System.IO;
  3 using UnityEditor;
  4 using UnityEngine;
  5 
  6 public class TextureCompressTool : ScriptableObject
  7 {
  8     [MenuItem("FashionBeat/Compress texture")]
  9     static void CompressSelectTexture()
 10     {
 11         List<string> list = new List<string>();
 12 
 13         foreach (var so in Selection.objects)
 14         {
 15             bool isFolder = so is DefaultAsset;
 16             string selectObjPath = AssetDatabase.GetAssetPath(so);
 17             if (isFolder)
 18             {
 19                 string[] files = Directory.GetFiles(selectObjPath, "*.*", SearchOption.AllDirectories);
 20                 for (int i = 0; i < files.Length; i++)
 21                 {
 22                     string filepath = files[i];
 23                     if (FBEditorHelper.IsImage(filepath))
 24                     {
 25                         string assetpath = FBEditorHelper.GetAssetPath(filepath);
 26                         if (!list.Contains(assetpath))
 27                             list.Add(assetpath);
 28                     }
 29                 }
 30             }
 31             else
 32             {
 33                 if (!list.Contains(selectObjPath))
 34                     list.Add(selectObjPath);
 35             }
 36         }
 37 
 38         for (int i = 0; i < list.Count; i++)
 39         {
 40             string assetpath = list[i];
 41             CompressTexture(assetpath);
 42             EditorUtility.DisplayProgressBar("批量处理图片", assetpath, (float)i / list.Count);
 43         }
 44 
 45         EditorUtility.ClearProgressBar();
 46         AssetDatabase.Refresh();
 47         Debug.Log("All done, count: " + list.Count);
 48     }
 49 
 50     static void CompressTexture(string assetpath)
 51     {
 52         // 根据路径获得文件目录,设置图集的packagingTag
 53         TextureImporter textureImporter = AssetImporter.GetAtPath(assetpath) as TextureImporter;
 54         CompressTexture(textureImporter);
 55     }
 56 
 57     public static void CompressTexture(TextureImporter textureImporter)
 58     {
 59         // 根据路径获得文件目录,设置图集的packagingTag
 60         if (!textureImporter)
 61         {
 62             Debug.LogWarning("textureImporter == null");
 63             return;
 64         }
 65         if (textureImporter.textureType != TextureImporterType.Default)
 66         {
 67             Debug.LogWarning("This texture is not Default Type: " + textureImporter.assetPath, textureImporter);
 68             return;
 69         }
 70         bool haveAlpha = textureImporter.DoesSourceTextureHaveAlpha();
 71 
 72         textureImporter.alphaIsTransparency = haveAlpha;
 73         textureImporter.mipmapEnabled = false;
 74         textureImporter.isReadable = false;
 75         textureImporter.textureType = TextureImporterType.Default;
 76         //textureImporter.wrapMode = TextureWrapMode.Clamp;
 77         textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;
 78         textureImporter.textureCompression = TextureImporterCompression.Compressed;
 79 
 80         // Android 端单独设置
 81         TextureImporterPlatformSettings settingAndroid = new TextureImporterPlatformSettings()
 82         {
 83             name = "Android",
 84             overridden = true,
 85             format = haveAlpha ? TextureImporterFormat.ETC2_RGBA8 : TextureImporterFormat.ETC_RGB4,
 86         };
 87         textureImporter.SetPlatformTextureSettings(settingAndroid);
 88 
 89         // IOS端单独设置
 90         TextureImporterPlatformSettings settingIOS = new TextureImporterPlatformSettings()
 91         {
 92             name = "iOS",
 93             overridden = true,
 94             format = TextureImporterFormat.ASTC_6x6,
 95         };
 96         textureImporter.SetPlatformTextureSettings(settingIOS);
 97 
 98         textureImporter.SaveAndReimport();
 99 
100         Debug.Log("Compress texture done: " + textureImporter.assetPath);
101     }
102 
103 }

 

导入自动设置:

 1 using UnityEditor;
 2 using UnityEngine;
 3 
 4 public class AssetImporterProcessor : AssetPostprocessor
 5 {
 6     void OnPostprocessTexture(Texture2D texture)
 7     {
 8         TextureCompressTool.CompressTexture(assetImporter as TextureImporter);
 9     }
10 }

 

 

转载请注明出处:https://www.cnblogs.com/jietian331/p/15433179.html

posted @ 2021-10-21 13:57  孤独の巡礼  阅读(601)  评论(0编辑  收藏  举报