Unity插件-Texture查找和參數(shù)優(yōu)化

需求:需要針對(duì)texture做修改,首先需要找到所有大于1024的圖集;針對(duì)texture的部分參數(shù)做處理;并都加上白名單

分析:倆個(gè)功能單獨(dú)做,都需要針對(duì)特定的文件夾做搜索耀盗,然后要對(duì)白名單進(jìn)行過濾认然,第一個(gè)拿到對(duì)象然后進(jìn)行尺寸的判定并返回就可以了;第二個(gè)是過濾掉之后并對(duì)參數(shù)進(jìn)行修改,然后保存酌伊。

邏輯不復(fù)雜腾窝,直接上代碼,大體邏輯和之前的audio文件的優(yōu)化差不多居砖,感興趣的話可以看看上一篇

直接上代碼了

using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class ModifyAllTextureSetting : EditorWindow
{
    public static string FilePath = "Assets";
    public static List<string> allTexture = new List<string>();//所有的Texture
    public static List<string> all1024Texture = new List<string>(); //大于1024尺寸的圖片
    public static List<string> allModifyTexture = new List<string>(); //所有需要修改的圖片
    Vector2 mScroll = Vector2.zero;
    
    //需要修改參數(shù)的文件夾
    static string[] modifySettingPath = new string[]{
        "Assets/MeiShuZiYuan/Atlas",
        "Assets/MeiShuZiYuan/Font",
        "Assets/MeiShuZiYuan_iOS/Atlas",
        "Assets/MeiShuZiYuan_iOS/Font",
    };
    //1024圖片白名單
    static string[] whiteList1024 = new string[]{
        "Assets/MeiShuZiYuan/Atlas",
        "Assets/MeiShuZiYuan/Font",
        "Assets/BundleFiles",        

    };
    //修改圖片白名單
    static string[] whiteListDefaultSetting = new string[]{
        "Assets/MeiShuZiYuan_iOS/Atlas",
        "Assets/MeiShuZiYuan_iOS/Font",
        "Assets/MeiShuZiYuan/Font",
        "Atlas_Activities",        
    };
    void OnGUI()
    {
        if(all1024Texture == null)
        {
            return;
        }        
        mScroll = GUILayout.BeginScrollView(mScroll);
        if(all1024Texture != null && all1024Texture.Count>0)
        {            
            if(NGUIEditorTools.DrawHeader("All1024Texture"))
            {                
                int count = 0;
                foreach(var item in all1024Texture)
                {
                    count +=1;
                    Texture tex = AssetDatabase.LoadAssetAtPath(item,typeof(Texture))as Texture;                    
                    GUIContent label2 = new GUIContent(count +"-"+item,item);
                    EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth*0.5f;
                    EditorGUILayout.ObjectField(label2,tex,typeof(Texture),false);
                }                
            }            
        }
        if(allModifyTexture != null && allModifyTexture.Count>0)
        {            
            if(NGUIEditorTools.DrawHeader("AllModifyTexture"))
            {                
                int count = 0;
                foreach(var item in allModifyTexture)
                {
                    count +=1;
                    Texture tex = AssetDatabase.LoadAssetAtPath(item,typeof(Texture))as Texture;                    
                    GUIContent label2 = new GUIContent(count +"-"+item,item);
                    EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth*0.7f;
                    EditorGUILayout.ObjectField(label2,tex,typeof(Texture),false);
                }                
            }            
        }
        GUILayout.EndScrollView();
    }
    [MenuItem("Assets/處理Texture/檢測(cè)Texture超過1024的")]
    public static void ScanAllFile()
    {
        //遍歷所有的texture
        //判斷尺寸大小 
        //加入1024的圖片
        allTexture.Clear();
        all1024Texture.Clear();
        string projectPath = Application.dataPath;
        projectPath = projectPath.Substring(0,projectPath.IndexOf("Assets"));
        try
        {
            GetTexturesPath(projectPath,FilePath,"Texture",ref allTexture);
        }
        catch (System.Exception e)
        {
             Debug.LogError(e);
        }
        for(int i = 0;i < allTexture.Count;i++)
        {
            EditorUtility.DisplayProgressBar("Check 1024Textures","Current 1024Texture :"+i+"/"+allTexture.Count,(float)i/allTexture.Count);
            try
            {
                 bool flag = false;
                for (int j =0;j < whiteList1024.Length; j++)
                {
                    if(allTexture[i].Contains(whiteList1024[j]))
                    {
                        flag = true;  
                        break;                     
                    }
                }
                if(!flag)
                {
                    BegainCheck1024Texs(allTexture[i]);                             
                }                                
            }
            catch (System.Exception e)
            {
                EditorUtility.ClearProgressBar();
                Debug.LogError(e); 
            }   
        }        
        EditorUtility.ClearProgressBar();
        EditorWindow.GetWindow<ModifyAllTextureSetting>(false,"ModifyAllTextureSetting",true).Show();
        Debug.Log("Check Success");
    }
    //開始檢查Tex
    public static void BegainCheck1024Texs(string texPath)
    {        
        Texture tex = AssetDatabase.LoadAssetAtPath(texPath,typeof(Texture))as Texture;        
        if(tex.width>1024 || tex.height>1024)
        {
            all1024Texture.Add(texPath);                            
        } 
        // Debug.Log("all1024Texture-----count-----"+all1024Texture.Count);
    }          
    //開始修改參數(shù)
    public static void BegainModifyTexs(string texPath)
    {                
        CompressTexture(texPath);        
    }                   
    public static void CompressTexture(string path)
    {
        Texture tex = AssetDatabase.LoadAssetAtPath(path,typeof(Texture))as Texture;        
        // 根據(jù)路徑獲得文件目錄虹脯,設(shè)置圖集的packagingTag
        TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
        if(!textureImporter)
        {
            Debug.LogError("textureImporter==============null");
            return;
        }
        if (textureImporter.textureType != TextureImporterType.Default)
        {
            Debug.LogWarning("This texture is not Default Type: " + textureImporter.assetPath, textureImporter);
            return;
        }
        bool flag = false;        
        if(!textureImporter.isReadable)
        {
            textureImporter.isReadable = false;
            flag = true;
        }
        if(!textureImporter.streamingMipmaps)
        {
            textureImporter.streamingMipmaps = false;
            flag = true;
        }
        if(!textureImporter.mipmapEnabled)
        {
            textureImporter.mipmapEnabled = false;
            flag = true;
        }
        if(textureImporter.wrapMode != TextureWrapMode.Clamp)
        {
            textureImporter.wrapMode = TextureWrapMode.Clamp;
            flag = true;
        }
        if(textureImporter.filterMode != FilterMode.Bilinear)
        {
            textureImporter.filterMode = FilterMode.Bilinear;
            flag = true;
        }
        TextureImporterPlatformSettings settingDefault = textureImporter.GetDefaultPlatformTextureSettings();
        if(settingDefault.format != TextureImporterFormat.Automatic)
        {
            settingDefault.format = TextureImporterFormat.Automatic;
            flag = true;
        }
        if(settingDefault.compressionQuality != (int)TextureCompressionQuality.Normal)
        {
            settingDefault.compressionQuality = (int)TextureCompressionQuality.Normal;
            flag = true;
        }
        if(flag)
        {
            allModifyTexture.Add(path);
            textureImporter.SaveAndReimport();
        }

    }
    //獲取所有的圖片文件的path信息
    public static void GetTexturesPath(string projectPath,string targetFilePath,string searchType,ref List<string> array)
    {
        if (Directory.Exists(targetFilePath))
        {
            string[] guids;
            //搜索
            guids = AssetDatabase.FindAssets("t:" + searchType, new[] { targetFilePath });            
            foreach (string guid in guids)
            {
                string source = AssetDatabase.GUIDToAssetPath(guid);
                string[] allStr = source.Split('.');                
                if(source.EndsWith(".bmp")|| source.EndsWith(".jpeg") || source.EndsWith(".jpg") || source.EndsWith(".png"))
                {
                    allTexture.Add(source);                     
                }                                
            }            
        }
    }  

    [MenuItem("Assets/處理Texture/設(shè)置圖片Default參數(shù)")]
    public static async void ApplyAllPlatformTexSetting()
    {
        allTexture.Clear();
        string projectPath = Application.dataPath;
        // Debug.Log("projectPath-------------"+projectPath);
        projectPath = projectPath.Substring(0,projectPath.IndexOf("Assets"));
        try
        {
            GetTexturesPathFromSpecifyPath(projectPath,FilePath,"Texture",ref allTexture,modifySettingPath);
        }
        catch (System.Exception e)
        {
             Debug.LogError(e);
        }
        for(int i = 0;i < allTexture.Count;i++)
        {
            EditorUtility.DisplayProgressBar("Modify AllTextures","Current Need Modify Texture :"+i+"/"+allTexture.Count,(float)i/allTexture.Count);
            try
            {
                bool flag = false;
                for (int j =0;j < whiteListDefaultSetting.Length; j++)
                {
                    if(allTexture[i].Contains(whiteListDefaultSetting[j]))
                    {
                        flag = true;  
                        break;                     
                    }
                }
                if(!flag)
                {
                    BegainModifyTexs(allTexture[i]);                                                           
                }                  
            }
            catch (System.Exception e)
            {
                EditorUtility.ClearProgressBar();
                Debug.LogError(e); 
            }   
        }        
        EditorUtility.ClearProgressBar();
        EditorWindow.GetWindow<ModifyAllTextureSetting>(false,"ModifyAllTextureSetting",true).Show();
        Debug.Log("Modify Success");        
    }

    //獲取指定文件夾的所有的圖片文件的path信息
    public static void GetTexturesPathFromSpecifyPath(string projectPath,string targetFilePath,string searchType,ref List<string> array,string[] searchPath)
    {
        if (Directory.Exists(targetFilePath))
        {
            string[] guids;
            //搜索
            guids = AssetDatabase.FindAssets("t:" + searchType, searchPath);            
            foreach (string guid in guids)
            {
                string source = AssetDatabase.GUIDToAssetPath(guid);
                string[] allStr = source.Split('.');                
                if(source.EndsWith(".bmp")|| source.EndsWith(".jpeg") || source.EndsWith(".jpg") || source.EndsWith(".png"))
                {
                    allTexture.Add(source);                     
                }                                
            }            
        }
    } 
}

參考1
參考2
參考3

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市奏候,隨后出現(xiàn)的幾起案子循集,更是在濱河造成了極大的恐慌,老刑警劉巖鼻由,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件暇榴,死亡現(xiàn)場(chǎng)離奇詭異厚棵,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蔼紧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門婆硬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人奸例,你說我怎么就攤上這事彬犯。” “怎么了查吊?”我有些...
    開封第一講書人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵谐区,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我逻卖,道長(zhǎng)宋列,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任评也,我火速辦了婚禮炼杖,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘盗迟。我一直安慰自己坤邪,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開白布罚缕。 她就那樣靜靜地躺著艇纺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪邮弹。 梳的紋絲不亂的頭發(fā)上黔衡,一...
    開封第一講書人閱讀 51,763評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音腌乡,去河邊找鬼员帮。 笑死,一個(gè)胖子當(dāng)著我的面吹牛导饲,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播氯材,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼渣锦,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了氢哮?” 一聲冷哼從身側(cè)響起袋毙,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎冗尤,沒想到半個(gè)月后听盖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體胀溺,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年皆看,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了仓坞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡腰吟,死狀恐怖无埃,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情毛雇,我是刑警寧澤嫉称,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站灵疮,受9級(jí)特大地震影響织阅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜震捣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一荔棉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧伍派,春花似錦江耀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至晾腔,卻和暖如春舌稀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背灼擂。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來泰國打工壁查, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人剔应。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓睡腿,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親峻贮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子席怪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容