/
FindReferencesInProject.cs
70 lines (60 loc) · 2.1 KB
/
FindReferencesInProject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class FindReferencesInProject
{
private const string MenuItemText = "Assets/Find References In Project";
[MenuItem(MenuItemText, false, 25)]
public static void Find()
{
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
var referenceCache = new Dictionary<string, List<string>>();
string[] guids = AssetDatabase.FindAssets("");
foreach (string guid in guids)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
string[] dependencies = AssetDatabase.GetDependencies(assetPath, false);
foreach (var dependency in dependencies)
{
if (referenceCache.ContainsKey(dependency))
{
if (!referenceCache[dependency].Contains(assetPath))
{
referenceCache[dependency].Add(assetPath);
}
}
else
{
referenceCache[dependency] = new List<string>(){ assetPath };
}
}
}
Debug.Log("Build index takes " + sw.ElapsedMilliseconds + " milliseconds");
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
Debug.Log("Find: " + path, Selection.activeObject);
if (referenceCache.ContainsKey(path))
{
foreach (var reference in referenceCache[path])
{
Debug.Log(reference, AssetDatabase.LoadMainAssetAtPath(reference));
}
}
else
{
Debug.LogWarning("No references");
}
referenceCache.Clear();
}
[MenuItem(MenuItemText, true)]
public static bool Validate()
{
if (Selection.activeObject)
{
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
return !AssetDatabase.IsValidFolder(path);
}
return false;
}
}