首页 >Unity3D频道 >【Unity杂文】 > 使用ripgrep快速搜索引用关系
2023
12-16

使用ripgrep快速搜索引用关系

反向搜索某个资源被哪里引用,比如贴图被那个资源使用了,可以利用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);
            }
        }
    }
}

 

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

使用ripgrep快速搜索引用关系》有 2 条评论

  1. 草莓声明说:

    如果是二进制的资产引用就不会被查到,这个原理应该还是查所有text文本

留下一个回复

你的email不会被公开。 必填项已用 * 标注。