Unity引擎內圖片高階選項批量修改
阿新 • • 發佈:2018-12-02
</pre><pre name="code" class="csharp">
using UnityEngine; using System.Collections; using UnityEditor; using System.IO; using System.Collections.Generic; /// <summary> /// 功能:引擎內圖片高階選項批量修改 /// 平臺:Unity4.6.4/C# /// 設計模式:命令模式(15) /// FileName:TextureManager /// Creator: Thewen /// CreateTime:2016/4/20 15:26:45 /// Description: /// History: /// </summary> ///<summary> ///命令基類 為子類提供處理具體邏輯的邏輯類 ///</summary> public abstract class Command { protected Receiver _receiver; public Command(Receiver receiver) { this._receiver = receiver; } public abstract void ExecuteAction(); } /// <summary> /// 命令模式關鍵類 (處理邏輯主體) /// </summary> public class Receiver { TextureImporterSettings _importsettings=new TextureImporterSettings(); bool _iscompress; string _texturetype; string _path; TextureImporterFormat _iosformat; TextureImporterFormat _androidformat; //預設 public Receiver(string texturetype, string path) { this._texturetype = texturetype; this._path = path; this._importsettings.npotScale = TextureImporterNPOTScale.ToNearest; this._importsettings.ApplyTextureType(TextureImporterType.Advanced, false); this._importsettings.readable = false; this._importsettings.mipmapEnabled = true; this._importsettings.lightmap = false; this._iscompress = true; } public Receiver(string texturetype, string path,bool iscompress,bool mipmapenabled ,bool lightmap) { this._texturetype = texturetype; this._path = path; this._importsettings.npotScale = TextureImporterNPOTScale.ToNearest; this._importsettings.ApplyTextureType(TextureImporterType.Advanced, false); this._importsettings.readable = false; this._importsettings.mipmapEnabled = mipmapenabled; this._importsettings.lightmap = lightmap; this._iscompress = iscompress; } /// <summary> /// 設定圖片的高階屬性 /// </summary> public void SetTextureAnvanceSetting( ) { ArrayList filePath = TextureManager.GetAssetPath(_texturetype, _path); for (int i = 0; i < filePath.Count; i++) { TextureManager.SetAdvanceForPath(filePath[i].ToString(),_iscompress,_importsettings,_iosformat,_androidformat); } } } /// <summary> /// 命令執行類 (便於命令的集中管理,為實現撤銷於恢復提供可能) /// </summary> public class Invoker { public Command _command; public List<Command> _commandlist; public Invoker(Command command) { this._command = command; } public Invoker(List<Command> commandlist) { this._commandlist = commandlist; } public void ExecuteCommandList() { for (int i = 0; i < _commandlist.Count; i++) { _commandlist[i].ExecuteAction(); } //_commandlist.ExecuteAction(); } public void ExecuteCommand() { _command.ExecuteAction(); } } //命令1 設定圖片的高階選項 public class SetAdvanceCommand : Command { public SetAdvanceCommand(Receiver receiver):base(receiver) { } public override void ExecuteAction() { _receiver.SetTextureAnvanceSetting(); } } public class TextureManager: AssetPostprocessor { #region 目錄選單介面定義 [MenuItem("TextureManager/壓縮/批量貼圖Advance設定")] static void SetTextureAdvance() { string fileType = "*.png"; string fileTypeTGA = "*.tga"; //通用命令執行 執行列表的需求 List<Command> Commandlist = new List<Command>(); //Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\CH\\CH_Evil_Bolt"))); //單獨測試 Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\CH"))); Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\EVN"))); Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\OBJ"))); Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\CH"))); Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\EVN"))); Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\OBJ"))); Invoker invoker = new Invoker(Commandlist); invoker.ExecuteCommandList(); } [MenuItem("TextureManager/壓縮/批量貼圖FX設定")] static void SetTextureFX() { string fileType = "*.png"; string fileTypeTGA = "*.tga"; //通用命令執行 執行列表的需求 List<Command> Commandlist = new List<Command>(); //Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\FX\\Texture\\Flares", true, false, false))); //單獨測試 Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\FX", true, false, false))); Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\FX", true, false, false))); Invoker invoker = new Invoker(Commandlist); invoker.ExecuteCommandList(); } [MenuItem("TextureManager/壓縮/批量貼圖AdvanceLightMap設定")] static void SetTextureLightMap() { string fileType = "*.exr"; //通用命令執行 執行列表的需求 List<Command> Commandlist = new List<Command>(); Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKScene", true, false,true))); Invoker invoker = new Invoker(Commandlist); invoker.ExecuteCommandList(); } [MenuItem("TextureManager/壓縮/批量貼圖AdvanceT4M設定")] static void SetTextureT4M() { string fileType = "*.png"; List<Command> Commandlist = new List<Command>(); Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\T4MOBJ\\Terrains\\Texture", true, false, false))); Invoker invoker = new Invoker(Commandlist); invoker.ExecuteCommandList(); //SetAdvanceByType(fileType, "\\T4MOBJ\\Terrains\\Texture", "closemipmap"); } [MenuItem("TextureManager/RGBA32/單張圖片修改")] static void SetRGBA32Texture() { var select = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets); string filepath= AssetDatabase.GetAssetPath(select[0] as Texture2D); TextureImporterSettings importsettings = new TextureImporterSettings(); importsettings.npotScale = TextureImporterNPOTScale.ToNearest; importsettings.ApplyTextureType(TextureImporterType.Advanced, false); importsettings.readable = false; importsettings.mipmapEnabled = false; importsettings.lightmap = false; SetAdvanceForPath(filepath,false, importsettings,new TextureImporterFormat(),new TextureImporterFormat() ); } [MenuItem("TextureManager/RGBA32/UI批量修改")] static void SetRGBA32TextureAll() { string fileType = "*.png"; string fileTypeTGA = "*.tga"; List<Command> Commandlist = new List<Command>(); //Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\FX\\Texture\\Flares", true, false, false))); //單獨測試 Commandlist.Add(new SetAdvanceCommand(new Receiver(fileType, "\\HKBaseRes\\HKModels\\UI", false, false, false))); Commandlist.Add(new SetAdvanceCommand(new Receiver(fileTypeTGA, "\\HKBaseRes\\HKModels\\UI", false, false, false))); Invoker invoker = new Invoker(Commandlist); invoker.ExecuteCommandList(); } #endregion #region TextureManager 圖片處理公開方法 public static void SetAdvanceForPath(string filePath, bool isCompress, TextureImporterSettings importSettings, TextureImporterFormat iosFormat, TextureImporterFormat androidFormat) { Texture2D m2d = AssetDatabase.LoadAssetAtPath(filePath, typeof(Texture2D)) as Texture2D; Debug.Log(m2d.width + "*" + m2d.height); //判斷是否是壓縮格式 if (isCompress) { if (IsETCOrPVRTCFormat(m2d, m2d.format)) { return; } } else { if (IsRPGA(m2d, m2d.format)) { return; } } TextureImporter texImporter = GetTextureSettings(filePath);//AssetImporter.GetAtPath(filePath) as TextureImporter; //TextureManager.GetTextureSettings(filePath); TextureImporterSettings tis = new TextureImporterSettings(); texImporter.ReadTextureSettings(tis); tis.npotScale = TextureImporterNPOTScale.ToNearest; //tis = _importsettings;//這種寫法有出莫名問題(不提倡) tis.npotScale = importSettings.npotScale; tis.ApplyTextureType(TextureImporterType.Advanced, false); tis.readable = importSettings.readable; tis.mipmapEnabled = importSettings.mipmapEnabled; tis.lightmap = importSettings.lightmap; //註釋掉Unity面板會有警告提示出現 //if (m2d.width != m2d.height) //{ // //tis.wrapMode = TextureWrapMode.Clamp; //對於不是 NOPT(是“non power of two”的縮寫)的貼圖 wrap mode 不能被設定為Repeat //} texImporter.SetTextureSettings(tis); if (TextureManager.JudgeTransparentPic(m2d.format)) { if (isCompress) { androidFormat = TextureImporterFormat.ETC2_RGBA8; iosFormat = TextureImporterFormat.PVRTC_RGBA4; } else { androidFormat = iosFormat = TextureImporterFormat.RGBA32; } } else { if (isCompress) { androidFormat = TextureImporterFormat.ETC_RGB4; iosFormat = TextureImporterFormat.PVRTC_RGB4; } else { iosFormat = androidFormat = TextureImporterFormat.RGB24; } } int mSize = Mathf.Max(m2d.width, m2d.height); mSize = Mathf.ClosestPowerOfTwo(mSize); texImporter.SetPlatformTextureSettings("Default", mSize, TextureImporterFormat.AutomaticCompressed); texImporter.SetPlatformTextureSettings("Android", mSize, androidFormat); texImporter.SetPlatformTextureSettings("iPhone", mSize, iosFormat); AssetDatabase.ImportAsset(filePath); } public static ArrayList GetAssetPath(string fileType, string _Path) { string DirectoryPath = Application.dataPath.Substring(0, Application.dataPath.IndexOf("Assets")); DirectoryInfo directoryInfo = new DirectoryInfo(Application.dataPath + _Path); ArrayList filePath = new ArrayList(); foreach (FileInfo fi in directoryInfo.GetFiles(fileType, SearchOption.AllDirectories)) { string path = fi.DirectoryName + "\\" + fi.Name; path = path.Remove(0, DirectoryPath.Length); path = path.Replace("\\", "/"); //Texture2D m2d = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D; //if(JudgeTransparentPic(m2d.format)) filePath.Add(path); } return filePath; } public static bool IsETCOrPVRTCFormat(Texture2D _m2d, TextureFormat _format) { string selfFormat = _format.ToString(); if (selfFormat == TextureFormat.ETC_RGB4.ToString() || selfFormat == TextureFormat.ETC2_RGBA8.ToString() || selfFormat == TextureFormat.PVRTC_RGB4.ToString() || selfFormat == TextureFormat.PVRTC_RGBA4.ToString()) { string path = AssetDatabase.GetAssetPath(_m2d); TextureImporter texImporter = GetTextureSettings(path);// AssetImporter.GetAtPath(path) as TextureImporter;// GetTextureSettings(path); if (texImporter.textureType != TextureImporterType.Advanced) { return false; } return true; } return false; } public static bool IsRPGA(Texture2D _m2d, TextureFormat _format) { string selfFormat = _format.ToString(); if (selfFormat == TextureFormat.RGBA32.ToString() || selfFormat == TextureFormat.RGB24.ToString()) { string path = AssetDatabase.GetAssetPath(_m2d); TextureImporter texImporter = GetTextureSettings(path);// AssetImporter.GetAtPath(path) as TextureImporter;//GetTextureSettings(path); if (texImporter.textureType != TextureImporterType.Advanced) { return false; } return true; } return false; } /// <summary> /// 判斷貼圖是否擁有透明通道 /// </summary> /// <param name="format"></param> /// <returns></returns> public static bool JudgeTransparentPic(TextureFormat format) { string selfFormat = format.ToString(); string[] transparentFormat = { TextureFormat.Alpha8.ToString(),TextureFormat.ARGB32.ToString(), TextureFormat.ARGB4444.ToString(),TextureFormat.ATC_RGBA8.ToString(), TextureFormat.ATF_RGBA_JPG.ToString(),TextureFormat.BGRA32.ToString(), TextureFormat.PVRTC_RGBA2.ToString(),TextureFormat.PVRTC_RGBA4.ToString(), TextureFormat.RGBA32.ToString(),TextureFormat.RGBA4444.ToString(), TextureFormat.DXT5.ToString(),TextureFormat.ETC2_RGBA1.ToString(), TextureFormat.ETC2_RGBA8.ToString(), }; for (int i = 0; i < transparentFormat.Length; i++) { if (selfFormat == transparentFormat[i]) { return true; } } return false; } /// <summary> /// 獲取貼圖設定 1024 size 這是一種保護 /// </summary> public static TextureImporter GetTextureSettings(string path) { TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; //Texture Type textureImporter.textureType = TextureImporterType.Advanced; //Non power of2 textureImporter.npotScale = TextureImporterNPOTScale.ToNearest; //PlatformTextureSettings textureImporter.SetPlatformTextureSettings("iPhone", 2048, TextureImporterFormat.PVRTC_RGBA4); textureImporter.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.ETC2_RGBA8); return textureImporter; } #endregion }