Skip to content

Instantly share code, notes, and snippets.

@zhutaorun
Forked from jringrose/FindProjectReferences.cs
Created December 26, 2016 03:09
Show Gist options
  • Save zhutaorun/c98841c432f0c2492291cf318a4d3c60 to your computer and use it in GitHub Desktop.
Save zhutaorun/c98841c432f0c2492291cf318a4d3c60 to your computer and use it in GitHub Desktop.
Unity editor extension that uses spotlight on OSX for lightning fast project reference searches. Asset serialization mode should be set to "Force Text" in the editor settings.
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
public class FindProject {
#if UNITY_EDITOR_OSX
[MenuItem("Assets/Find References In Project", false, 2000)]
private static void FindProjectReferences()
{
string appDataPath = Application.dataPath;
string output = "";
string selectedAssetPath = AssetDatabase.GetAssetPath (Selection.activeObject);
List<string> references = new List<string>();
string guid = AssetDatabase.AssetPathToGUID (selectedAssetPath);
var psi = new System.Diagnostics.ProcessStartInfo();
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
psi.FileName = "/usr/bin/mdfind";
psi.Arguments = "-onlyin " + Application.dataPath + " " + guid;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = psi;
process.OutputDataReceived += (sender, e) => {
if(string.IsNullOrEmpty(e.Data))
return;
string relativePath = "Assets" + e.Data.Replace(appDataPath, "");
// skip the meta file of whatever we have selected
if(relativePath == selectedAssetPath + ".meta")
return;
references.Add(relativePath);
};
process.ErrorDataReceived += (sender, e) => {
if(string.IsNullOrEmpty(e.Data))
return;
output += "Error: " + e.Data + "\n";
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(2000);
foreach(var file in references){
output += file + "\n";
Debug.Log(file, AssetDatabase.LoadMainAssetAtPath(file));
}
Debug.LogWarning(references.Count + " references found for object " + Selection.activeObject.name + "\n\n" + output);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment