6

使用ripgrep快速搜索引用关系

 5 months ago
source link: https://www.xuanyusong.com/archives/5073
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用ripgrep快速搜索引用关系

雨松MOMO Unity3D频道 围观4602 条评论 编辑日期:2023-12-16 字体:

反向搜索某个资源被哪里引用,比如贴图被那个资源使用了,可以利用ripgrep这样搜索会秒出结果。如下图所示,拖入待搜索的贴图,然后搜索它被哪里用了。

使用ripgrep快速搜索引用关系 - 雨松MOMO程序研究院 - 1

rg.exe 下载  https://github.com/BurntSushi/ripgrep/releases

代码利用 rg.exe -l guid 搜索,最终显示在界面中

public class SearchWindows : EditorWindow
    [MenuItem ("Assets/Search Reference")]
    public static void OpenWindow ()
        (EditorWindow.GetWindow(typeof(SearchWindows)) as SearchWindows).Search = Selection.activeObject;
    public Object Search;
    List<Object> SearchOut= new List<Object>();
    private void OnGUI()
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label ("Search:", EditorStyles.boldLabel);
        Search = EditorGUILayout.ObjectField(Search, typeof(Object), true);
        EditorGUILayout.EndHorizontal();
        if (GUILayout.Button("Search!"))
            if (Search != null)
                SearchOut.Clear();
                List<string> @out = new List<string>();
                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 = $"{Application.dataPath}/../../Tools/Search/rg.exe";
                    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)
                                SearchOut.Add(item);
        if (SearchOut.Count > 0)
            GUILayout.Label("Out:", EditorStyles.boldLabel);
            foreach (var o in SearchOut)
                EditorGUILayout.ObjectField(o, typeof(Object), true);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK