查找任意shader的Material引用粮揉,并在界面上顯示出來抚笔,且可以快速定位

1首先要遍歷所有的資源

過濾出.mat的資源,也就是Material,然后通過Shader名來查找對應的引用的材質(zhì)殊橙,然后可以將這些路徑名打出來。

改進

打印出所有的路徑名還要自己去找不是很方便膨蛮,后面將它輸出到面板上,并且可以快速定位敞葛,用到了編輯器開發(fā),editorlayout,

ObjectField

核心邏輯
EditorGUILayout.ObjectField("", go, typeof(Material), false);                    

最后因為路徑太長需要修改左側的長度惹谐,后面引入了GUIContent作為第一個參數(shù)驼卖,然后以為需要長度長一點氨肌,
引入邏輯EditorGUIUtility.labelWidth = 400酌畜,設置他的長度。

最后上代碼

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using System.Threading;
 
public class SearchShader :EditorWindow
{
 
    // public static string FilePath = "Assets/Materials";
    public static string FilePath = "Assets";
    //搜索固定文件夾中的所有Material的路徑
    public static List<string> listMatrials;
    public static List<string> listTargetMaterial;
 
    public static string selectedShaderName;
 
    public static StringBuilder sb;
    static public SearchShader instance;
    public Dictionary<string, BetterList<string>> dict;
    public BetterList<string> allPath;
    Vector2 mScroll = Vector2.zero;
    
    void OnEnable()
    {
        // instance = this;
    }

    void OnDisable()
    {
        // instance = null;
    }

    void OnGUI()
    {
        if (listTargetMaterial == null)
        {
            return;
        }

        mScroll = GUILayout.BeginScrollView(mScroll);

        List<string> list = listTargetMaterial;
        if (list != null && list.Count > 0)
        {
            if (NGUIEditorTools.DrawHeader("Mat"))
            {
                int count = 0;
                foreach (string item in list)
                {
                    // Debug.Log("item ----------------"+item);                    
                    count += 1;
                    Material go = AssetDatabase.LoadAssetAtPath(item, typeof(Material)) as Material;                    
                    GUIContent label1 = new GUIContent(count+"---"+item,item);                                                    
                    EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth * 0.6f;                                               
                    EditorGUILayout.ObjectField(label1, go, typeof(Material), false);                    
                }
            }
            // Debug.Log("當前的窗體的的寬度是----------------------------------"+EditorGUIUtility.currentViewWidth);

            list = null;
        }        

        GUILayout.EndScrollView();
    }

    [MenuItem("Assets/SearchShader", true)]
    private static bool OptionSelectAvailable()
    {
        if(Selection.activeObject == null)
        {
            return false;
        }
        return Selection.activeObject.GetType() == typeof(Shader);
    }
 
    [MenuItem("Assets/SearchShader")]
    private static void SearchConstantShader()
    {
        Debug.Log("當前選中的Shader名字:" + Selection.activeObject.name);
        sb = new StringBuilder();
 
        selectedShaderName = Selection.activeObject.name;
 
        listMatrials = new List<string>();
        listMatrials.Clear();
        listTargetMaterial = new List<string>();
        listTargetMaterial.Clear();
 
        //項目路徑 eg:projectPath = D:Project/Test/Assets
        string projectPath = Application.dataPath;
 
        //eg:projectPath = D:Project/Test
        projectPath = projectPath.Substring(0, projectPath.IndexOf("Assets"));
 
        try
        {
            //獲取某一文件夾中的所有Matrial的Path信息
            GetMaterialsPath(projectPath, FilePath, "Material",ref listMatrials);
        }
        catch(System.Exception e)
        {
            Debug.LogError(e);
        }
 
        for (int i = 0; i < listMatrials.Count; i++)
        {
            EditorUtility.DisplayProgressBar("Check Materials", "Current Material :" 
                + i + "/" + listMatrials.Count,(float)i/ listMatrials.Count);
 
            try
            {
                //開始Check
                BegainCheckMaterials(listMatrials[i]);
            }
            catch (System.Exception e)
            {
                EditorUtility.ClearProgressBar();
                Debug.LogError(e);
            }
        }
 
        // PrintToTxt();
        //生成界面
        EditorUtility.ClearProgressBar();
        EditorWindow.GetWindow<SearchShader>(false, "SearchShader", true).Show();
        Debug.Log("Check Success");
    }
 
    //獲取某一文件夾中的所有Matrial的Path信息
    public static void GetMaterialsPath(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(allStr[allStr.Length-1] == "mat")
                if(source.EndsWith(".mat"))
                {
                    listMatrials.Add(source);
                    // Debug.Log("當前的material------------"+source);
                }                                
            }
            // Debug.Log("當前material的總數(shù)是---------------"+listMatrials.Count);
        }
    }
 
    //開始檢查Material
    public static void BegainCheckMaterials(string materialPath)
    {
        Material mat = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
        if (mat.shader.name == selectedShaderName)
        {
            listTargetMaterial.Add(materialPath);
        }
    }
 
    public static void PrintToTxt()
    {
        //加入shader的名字
        listTargetMaterial.Add(selectedShaderName);
 
        FileInfo fi = new FileInfo(Application.dataPath + "/Materials.txt");
        if (!fi.Exists)
        {
            // fi.CreateText();   
            // fi.OpenWrite();  
            // Thread.Sleep(1000);              
            var stream =  fi.Create();
            stream.Dispose();  
        }
        // else
        {
            StreamWriter sw = new StreamWriter(Application.dataPath + "/Materials.txt");
            for (int i = 0; i < listTargetMaterial.Count - 1; i++)
            {
                sb.Append(listTargetMaterial[i] + "\n");
            }
            string useNum = string.Format("共有 {0} 個Material用到:{1}", listTargetMaterial.Count - 1, selectedShaderName);
            sb.Append(useNum + "\n");
            sb.Append("用到的shader名字為:" + selectedShaderName);
            sw.Write(sb.ToString());
 
            sw.Flush();
            sw.Close();
            sw.Dispose();
        }
    }

}

ps:腳本放在Assets下面的Editor下面,沒有就創(chuàng)建一個

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末井誉,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子颗圣,更是在濱河造成了極大的恐慌,老刑警劉巖在岂,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蔽午,居然都是意外死亡,警方通過查閱死者的電腦和手機及老,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來食铐,“玉大人,你說我怎么就攤上這事虐呻。” “怎么了斟叼?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長朗涩。 經(jīng)常有香客問我,道長谢床,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任萤悴,我火速辦了婚禮,結果婚禮上覆履,老公的妹妹穿的比我還像新娘。我一直安慰自己硝全,他們只是感情好栖雾,可當我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布伟众。 她就那樣靜靜地躺著,像睡著了一般凳厢。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上先紫,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天,我揣著相機與錄音遮精,去河邊找鬼。 笑死本冲,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的檬洞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼疮胖,長吁一口氣:“原來是場噩夢啊……” “哼闷板!你這毒婦竟也來了?” 一聲冷哼從身側響起遮晚,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎县遣,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體萧求,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年夸政,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡坑资,死狀恐怖穆端,靈堂內(nèi)的尸體忽然破棺而出袱贮,到底是詐尸還是另有隱情体啰,我是刑警寧澤攒巍,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布荒勇,位于F島的核電站,受9級特大地震影響枕屉,放射性物質(zhì)發(fā)生泄漏常柄。R本人自食惡果不足惜搀擂,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望哨颂。 院中可真熱鬧,春花似錦威恼、人聲如沸品姓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽斤蔓。三九已至,卻和暖如春弦牡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背驾锰。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留椭豫,地道東北人买喧。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像淤毛,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子算柳,可洞房花燭夜當晚...
    茶點故事閱讀 45,092評論 2 355

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