using DG.Tweening.Plugins.Core.PathCore; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; public class SearchWindows : EditorWindow { [MenuItem("Assets/Search Reference")] public static void OpenWindow() { (EditorWindow.GetWindow(typeof(SearchWindows)) as SearchWindows).Search = Selection.activeObject; } public static string assetSrcFolderPath; public static string assetSrcFolderAllPath; public Object Search; List SearchOut = new List(); List SearchOutAll = new List(); List SearchOutOne = new List(); Vector2 scrollPosition1; Vector2 scrollPosition2; private void OnGUI() { SearchOne(); SearchAllNo(); SearchAllIn(); } void SearchOne() { EditorGUILayout.BeginHorizontal(); GUILayout.Label("Search:", EditorStyles.boldLabel); Search = EditorGUILayout.ObjectField(Search, typeof(Object), true); EditorGUILayout.EndHorizontal(); if (GUILayout.Button("Search!")) { if (Search != null) { SearchOutOne.Clear(); List @out = new List(); string path = AssetDatabase.GetAssetPath(Search); if (!string.IsNullOrEmpty(path)) { string guid = AssetDatabase.AssetPathToGUID(path); string meta = AssetDatabase.GetTextMetaFilePathFromAssetPath(path); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.WorkingDirectory = Application.dataPath; p.StartInfo.FileName = $"rg "; p.StartInfo.Arguments = $"-l {guid}"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start(); while (!p.StandardOutput.EndOfStream) { string line = $"Assets/{p.StandardOutput.ReadLine().Replace("\\", "/")}"; if (line != meta) { var item = AssetDatabase.LoadAssetAtPath(line, typeof(Object)); if (item != null) SearchOutOne.Add(item); } } } } } if (SearchOutOne.Count > 0) { scrollPosition2 = EditorGUILayout.BeginScrollView(scrollPosition2); GUILayout.Label($"Out: {SearchOutOne.Count}", EditorStyles.boldLabel); foreach (var o in SearchOutOne) { EditorGUILayout.ObjectField(o, typeof(Object), true); } EditorGUILayout.EndScrollView(); } } void SearchAllNo() { EditorGUILayout.BeginHorizontal(); //assetSrcFolderPath = EditorGUILayout.TextField(assetSrcFolderPath); //if (GUILayout.Button("选择")) //{ // assetSrcFolderPath = EditorUtility.OpenFolderPanel("选择文件夹", assetSrcFolderPath, ""); //} // 获取拖进去的文件夹路径 if (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (Event.current.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); foreach (string path in DragAndDrop.paths) { if (Directory.Exists(path)) { assetSrcFolderPath = path; break; } } } } // 显示文件夹路径 EditorGUILayout.LabelField("Folder Path: " + assetSrcFolderPath); EditorGUILayout.EndHorizontal(); if (GUILayout.Button("开始查询没有被引用") && assetSrcFolderPath != null) { SearchOut.Clear(); var files = Directory.GetFiles(assetSrcFolderPath, "*.*").Where(file => !file.EndsWith(".meta")).ToArray(); bool isHas = false; foreach (var path in files) { if (!string.IsNullOrEmpty(path)) { isHas = false; string guid = AssetDatabase.AssetPathToGUID(path); string meta = AssetDatabase.GetTextMetaFilePathFromAssetPath(path); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.WorkingDirectory = Application.dataPath; p.StartInfo.FileName = $"rg "; p.StartInfo.Arguments = $"-l {guid}"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start(); while (!p.StandardOutput.EndOfStream) { string line = $"Assets/{p.StandardOutput.ReadLine().Replace("\\", "/")}"; if (line != meta && !line.Contains("AssetGroups")) { var item = AssetDatabase.LoadAssetAtPath(line, typeof(Object)); if (item != null) { isHas = true; break; } } } if (!isHas) { SearchOut.Add(AssetDatabase.LoadAssetAtPath(path, typeof(Object))); } } } } if (SearchOut.Count > 0) { scrollPosition1 = EditorGUILayout.BeginScrollView(scrollPosition1); GUILayout.Label($"Out: {SearchOut.Count}", EditorStyles.boldLabel); foreach (var o in SearchOut) { EditorGUILayout.ObjectField(o, typeof(Object), true); } EditorGUILayout.EndScrollView(); } } void SearchAllIn() { EditorGUILayout.BeginHorizontal(); // 获取拖进去的文件夹路径 if (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (Event.current.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); foreach (string path in DragAndDrop.paths) { if (Directory.Exists(path)) { assetSrcFolderAllPath = path; break; } } } } // 显示文件夹路径 EditorGUILayout.LabelField("Folder Path: " + assetSrcFolderAllPath); EditorGUILayout.EndHorizontal(); if (GUILayout.Button("开始查询引用") && assetSrcFolderAllPath != null) { SearchOutAll.Clear(); var files = Directory.GetFiles(assetSrcFolderAllPath, "*.*").Where(file => !file.EndsWith(".meta")).ToArray(); foreach (var path in files) { if (!string.IsNullOrEmpty(path)) { string guid = AssetDatabase.AssetPathToGUID(path); string meta = AssetDatabase.GetTextMetaFilePathFromAssetPath(path); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.WorkingDirectory = Application.dataPath; p.StartInfo.FileName = $"rg "; p.StartInfo.Arguments = $"-l {guid}"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start(); while (!p.StandardOutput.EndOfStream) { string line = $"Assets/{p.StandardOutput.ReadLine().Replace("\\", "/")}"; if (line != meta && !line.Contains("AssetGroups")) { var item = AssetDatabase.LoadAssetAtPath(line, typeof(Object)); if (item != null) { SearchOutAll.Add(item); Debug.Log($"SearchOutAll: {item.name} ==> {path}"); } } } } } } if (SearchOutAll.Count > 0) { scrollPosition1 = EditorGUILayout.BeginScrollView(scrollPosition1); GUILayout.Label($"Out: {SearchOutAll.Count}", EditorStyles.boldLabel); foreach (var o in SearchOutAll) { EditorGUILayout.ObjectField(o, typeof(Object), true); } EditorGUILayout.EndScrollView(); } } }