Skip to content

Instantly share code, notes, and snippets.

@jringrose
Last active June 17, 2022 21:34
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jringrose/617d4cba87757591ce28 to your computer and use it in GitHub Desktop.
Save jringrose/617d4cba87757591ce28 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.
/*
MIT License
Copyright (c) 2016 Jesse Ringrose
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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
}
@zhaoqingqing
Copy link

have windows version?

@networm
Copy link

networm commented Oct 31, 2018

@zhaoqingqing networm/FindReferencesInProject2: Find asset references super fast in Unity project. Both on macOS and Windows.
https://github.com/networm/FindReferencesInProject2

I have extended it to Windows platform by using ripgrep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment