using UnityEngine; using UnityEditor; using System.Collections.Generic; using System.IO; using System.Linq; public class SelectShaderDependencies : EditorWindow { Vector2 scrollPos = Vector2.zero; static private List foundedShaderResources = new List(); //所有使用资源 [MenuItem("Assets/查找Shader引用", true)] static bool SelectShaderDependenciesAvailable() { return Selection.activeObject != null && Selection.activeObject.GetType() == typeof(Shader); } [MenuItem("Assets/查找Shader引用", false)] public static void GetShaderDependencies() { foundedShaderResources.Clear(); List includeExtensions = new List() { ".mat" }; string[] files = Directory.GetFiles(Application.dataPath + "/LuaFramework/AssetBundleRes/scene/", "*.*", SearchOption.AllDirectories) .Where(s => includeExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray(); string path = AssetDatabase.GetAssetPath(Selection.activeObject); float count = 0; foreach (string file in files) { count++; EditorUtility.DisplayProgressBar("Processing...", "检索中....", count / files.Length); string strFile = file.Substring(file.IndexOf("Asset")).Replace('\\', '/'); string[] dependenciesFiles = AssetDatabase.GetDependencies(strFile, false); foreach (string depFile in dependenciesFiles) { if(path.Equals(depFile)) { foundedShaderResources.Add(strFile); } } } EditorUtility.ClearProgressBar(); if (foundedShaderResources.Count == 0) { EditorUtility.DisplayDialog("搜索结果", "没有找到任何材质球引用该Shader", "确定"); } else { EditorUtility.DisplayDialog("搜索结果", "有" + foundedShaderResources.Count + "个材质球使用了这个Shader", "确定"); SelectShaderDependencies checkWin = (SelectShaderDependencies)EditorWindow.GetWindow(false, "材质球搜索结果", true); checkWin.autoRepaintOnSceneChange = true; checkWin.Show(); } } void OnGUI() { ShowShaderDependenciesWindow(); } void ShowShaderDependenciesWindow() { EditorGUILayout.BeginHorizontal(); GUI.backgroundColor = Color.green; EditorGUILayout.EndHorizontal(); GUI.backgroundColor = Color.white; scrollPos = EditorGUILayout.BeginScrollView(scrollPos); foreach (var path in foundedShaderResources) { EditorGUILayout.BeginHorizontal(); Material t = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material)); EditorGUILayout.ObjectField(t, typeof(Material), false); GUI.backgroundColor = Color.green; if (GUILayout.Button("查找引用的模型", GUILayout.Width(150))) { SelectMaterialrDependencies.GetMaterialDependencies(path); } GUI.backgroundColor = Color.white; EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndScrollView(); } }