Unity Find References
2019-04-01 / 1 min read
Find references in Assets:
[MenuItem("Assets/Find Refereces")]
public static void FindReferences()
{
EditorSettings.serializationMode = SerializationMode.ForceText;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (!string.IsNullOrEmpty(path))
{
string guid = AssetDatabase.AssetPathToGUID(path);
List<string> withoutExtensions = new List<string>() {".prefab", ".unity", ".mat", ".asset"};
string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
<!-- more -->
.Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
int startIndex = 0;
EditorApplication.update = delegate()
{
string file = files[startIndex];
bool isCancel =
EditorUtility.DisplayCancelableProgressBar("Matching..." + startIndex + "/" + files.Length, file,
(float) startIndex / (float) files.Length);
if (Regex.IsMatch(File.ReadAllText(file), guid))
{
var assetpath = "Assets" + file.Replace(Application.dataPath, "");
Debug.Log(assetpath);
Debug.Log(file,
AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetpath));
}
startIndex++;
if (isCancel || startIndex >= files.Length)
{
EditorUtility.ClearProgressBar();
EditorApplication.update = null;
startIndex = 0;
Debug.Log("Matching Finished");
}
};
}
}