生活随笔
收集整理的這篇文章主要介紹了
Unity 如何实现批量修改图片格式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
unity開發游戲過程中,經常會批量修改部分圖片格式,比如ios端,我們會修改為astc6x6,android端我們會修改為ETC2。又或者我們需要把圖片打包圖集,需要批量設置SpriteTag。但是當我們導入大量圖片時,如果一個文件夾一個文件夾的修改,再加上android和ios不同平臺的修改,這就需要大量的操作,而且最要命的時修改完后,如果團隊里有人再添加新的圖片,上線打包時,還需要重新整理標記圖片格式。
那么就需要我們寫一個小工具來自動處理這個過程。常用的有兩種方式
第一種方式:
使用unity自帶的AssetPostprocessor的生命周期,這種方式是在圖片導入時或者導入前,介入編輯器的導入回調方法,然后自動設置所有導入圖片的格式屬性等,這種方法在每次導入都會自動調用,很方便,但是缺點就是,強制性,后續無法手動再調整圖片格式了,所以就不太靈活了,因為我們不能保證所有圖片都是用一種尺寸和格式。
基本結構如下:
using System
.Collections
;
using UnityEditor
;
public class MyEditor : AssetPostprocessor {public void OnPreprocessModel(){Debug
.Log
("OnPreprocessModel="+this.assetPath
);}public void OnPostprocessModel(GameObject go
){Debug
.Log
("OnPostprocessModel="+go
.name
);}public void OnPreprocessTexture(){Debug
.Log
("OnPreProcessTexture="+this.assetPath
);TextureImporter impor
= this.assetImporter
as TextureImporter
;impor
.textureFormat
= TextureImporterFormat
.ARGB32
;impor
.maxTextureSize
= 512;impor
.textureType
= TextureImporterType
.Advanced
;impor
.mipmapEnabled
= false;}public void OnPreprocessAudio(){AudioImporter audio
= this.assetImporter
as AudioImporter
;audio
.format
= AudioImporterFormat
.Compressed
;}public static void OnPostprocessAllAssets(string[]importedAsset
,string[] deletedAssets
,string[] movedAssets
,string[]movedFromAssetPaths
){Debug
.Log
("OnPostprocessAllAssets");foreach (string str
in importedAsset
) {Debug
.Log("importedAsset = "+str
);}foreach (string str
in deletedAssets
) {Debug
.Log("deletedAssets = "+str
);}foreach (string str
in movedAssets
) {Debug
.Log("movedAssets = "+str
);}foreach (string str
in movedFromAssetPaths
) {Debug
.Log("movedFromAssetPaths = "+str
);}}
}
這種方式雖然不靈活,但是我們也可以針對我們自己的項目進行深度定制,比如我們可以對不同文件夾和不同文件名進行不同格式設置。但是代碼量會比較多,如果有興趣可以訪問:https://zhuanlan.zhihu.com/p/88568714
第二種方式
使用自定義編輯器按鈕,比如:選中圖片文件夾時,對選中的文件夾下所有圖片進行處理,比較靈活,速度也快。我們可以設置完畢后,再手動微調下特殊的圖片格式。
using System
;
using System
.IO
;
using UnityEngine
;
using UnityEditor
;
public class AssetProcessor : AssetPostprocessor
{[MenuItem("Assets/Custom Reimport Images")]public static void SetAllTextureType(){var arr
= Selection
.GetFiltered(typeof(DefaultAsset
), SelectionMode
.Assets
);string folder
= AssetDatabase
.GetAssetPath(arr
[0]);Debug
.Log("Reimport Path:" + folder
);DirectoryInfo direction
= new DirectoryInfo(folder
);FileInfo
[] pngFiles
= direction
.GetFiles("*.png", SearchOption
.AllDirectories
);FileInfo
[] jpgfiles
= direction
.GetFiles("*.jpg", SearchOption
.AllDirectories
);try{SetTexture(pngFiles
);SetTexture(jpgfiles
);}catch (Exception e
){Debug
.LogError(e
);}finally{EditorUtility
.ClearProgressBar();AssetDatabase
.Refresh();}}static void SetTexture(FileInfo
[] fileInfo
){for (int i
= 0; i
< fileInfo
.Length
; i
++){string fullpath
= fileInfo
[i
].FullName
.Replace("\\","/");string path
= fullpath
.Replace(Application
.dataPath
, "Assets");TextureImporter textureImporter
= AssetImporter
.GetAtPath(path
) as TextureImporter
;EditorUtility
.DisplayProgressBar("批量處理圖片",fileInfo
[i
].Name
,i
/(float)fileInfo
.Length
);SetTextureFormat(textureImporter
);}}static void SetTextureFormat(TextureImporter textureImporter
){string AtlasName
= new DirectoryInfo(Path
.GetDirectoryName(textureImporter
.assetPath
)).Name
;textureImporter
.mipmapEnabled
= false;textureImporter
.isReadable
= false;textureImporter
.textureType
= TextureImporterType
.Sprite
;textureImporter
.spritePackingTag
= AtlasName
;textureImporter
.wrapMode
= TextureWrapMode
.Clamp
;textureImporter
.npotScale
= TextureImporterNPOTScale
.None
;textureImporter
.textureFormat
= TextureImporterFormat
.Automatic
;textureImporter
.textureCompression
= TextureImporterCompression
.Compressed
;TextureImporterPlatformSettings setting_android
= new TextureImporterPlatformSettings();setting_android
.overridden
= true;setting_android
.name
= "Android";if (textureImporter
.DoesSourceTextureHaveAlpha())setting_android
.format
= TextureImporterFormat
.ETC2_RGBA8
;elsesetting_android
.format
= TextureImporterFormat
.ETC2_RGB4
;textureImporter
.SetPlatformTextureSettings(setting_android
);TextureImporterPlatformSettings setting_iphone
= new TextureImporterPlatformSettings();setting_iphone
.overridden
= true;setting_iphone
.name
= "iOS";if (textureImporter
.DoesSourceTextureHaveAlpha())setting_android
.format
= TextureImporterFormat
.ASTC_RGBA_6x6
;elsesetting_android
.format
= TextureImporterFormat
.ASTC_RGB_6x6
;textureImporter
.SetPlatformTextureSettings(setting_iphone
);}
}
期望這篇文章對你有所幫助。
總結
以上是生活随笔為你收集整理的Unity 如何实现批量修改图片格式的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。