Skip to content

Instantly share code, notes, and snippets.

@q8f13
Created January 4, 2024 06:36
Show Gist options
  • Save q8f13/6ffd8ce00c111b800b61c6c8ceffef9b to your computer and use it in GitHub Desktop.
Save q8f13/6ffd8ce00c111b800b61c6c8ceffef9b to your computer and use it in GitHub Desktop.
Unity Util - find references via ripgrep
// from https://www.xuanyusong.com/archives/5073
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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment