using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System; using System.IO; using System.Text.RegularExpressions; public class TextureRefrenceFinder : EditorWindow { static TextureRefrenceFinder window; [UnityEditor.MenuItem("美术工具/贴图引用查找工具")] static void Init() { window = GetWindow(typeof(TextureRefrenceFinder), false, "贴图引用查找工具") as TextureRefrenceFinder; window.Show(); window.autoRepaintOnSceneChange = true; } static List selectedTextures = new List(); static Texture selectedTexture; static bool findAllMaterials = false; static List allMaterials = new List(); static List refrenceMaterials = new List(); [MenuItem("Assets/选择图片")] public static void SetSelectedTextures() { var assets = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets); var total = assets.Length; var index = 0; try { selectedTextures.Clear(); foreach (var asset in assets) { var assetPath = AssetDatabase.GetAssetPath(asset); var texture = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture)); if (texture != null) { selectedTextures.Add((Texture)texture); } index++; EditorUtility.DisplayProgressBar("查找图片", assetPath, index / (float)total); } } catch (Exception ex) { Debug.LogError(ex); } finally { selectedTexture = selectedTextures != null && selectedTextures.Count > 0 ? selectedTextures[0] : null; EditorUtility.ClearProgressBar(); } } Vector2 textureScrollViewPosition = Vector2.zero; Vector2 materialScrollViewPosition = Vector2.zero; private void OnGUI() { EditorGUILayout.BeginVertical(); EditorGUILayout.LabelField("贴图"); EditorGUILayout.Space(); if (selectedTextures != null && selectedTextures.Count > 0) { EditorGUI.indentLevel++; textureScrollViewPosition = EditorGUILayout.BeginScrollView(textureScrollViewPosition); foreach (var texture in selectedTextures) { DrawTexture(texture); } EditorGUILayout.EndScrollView(); EditorGUI.indentLevel--; } EditorGUILayout.Space(); if (GUILayout.Button("查找材质")) { if (!findAllMaterials) { FindAllMaterials(); } FindRefreceMaterials(selectedTexture); } EditorGUILayout.Space(); EditorGUILayout.LabelField("引用贴图的材质"); EditorGUILayout.Space(); if (refrenceMaterials != null && refrenceMaterials.Count > 0) { EditorGUI.indentLevel++; materialScrollViewPosition = EditorGUILayout.BeginScrollView(materialScrollViewPosition); foreach (var material in refrenceMaterials) { DrawMaterial(material); } EditorGUILayout.EndScrollView(); EditorGUI.indentLevel--; } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.EndVertical(); } private void DrawTexture(Texture _texture) { if (_texture == null) { return; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.ObjectField(_texture.name, _texture, typeof(Texture), true); if (EditorGUILayout.Toggle(_texture != null && _texture == selectedTexture)) { selectedTexture = _texture; } EditorGUILayout.EndHorizontal(); } private void DrawMaterial(Material _material) { if (_material == null) { return; } EditorGUILayout.ObjectField(_material.name, _material, typeof(Material), true); } static void FindAllMaterials() { var path = Application.dataPath + "/Art"; var allFiles = new DirectoryInfo(path).GetFiles("*", SearchOption.AllDirectories); var total = allFiles.Length; var index = 0; try { allMaterials.Clear(); foreach (var file in allFiles) { var assetPath = "Assets" + file.FullName.Replace("\\", "/").Replace(Application.dataPath, ""); var material = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Material)); if (material != null) { allMaterials.Add((Material)material); } index++; EditorUtility.DisplayProgressBar("初始化材质", assetPath, index / (float)total); } } catch (Exception ex) { Debug.LogError(ex); } finally { findAllMaterials = true; EditorUtility.ClearProgressBar(); } } static void FindRefreceMaterials(Texture _texture) { refrenceMaterials.Clear(); if (_texture == null) { return; } var guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(_texture)); var total = allMaterials.Count; var index = 0; try { foreach (var material in allMaterials) { var text = File.ReadAllText(AssetDatabase.GetAssetPath(material)); if (!string.IsNullOrEmpty(text) && Regex.IsMatch(text, guid)) { refrenceMaterials.Add(material); break; } index++; EditorUtility.DisplayProgressBar("查找引用材质", material.name, index / (float)total); } } catch (Exception ex) { Debug.Log(ex); } finally { EditorUtility.ClearProgressBar(); } } }