-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathWhatUsesThis.cs
150 lines (120 loc) · 4.03 KB
/
WhatUsesThis.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
namespace UnityEngine
{
public static class WhatUsesThis
{
const string CacheFilename = "Temp/WhatUsesThis.bin";
static Dictionary<string, List<string>> _dict;
static Dictionary<string, List<string>> Dict => _dict ?? Load() ?? CleanBuild();
[MenuItem( "Window/What Uses This/Rebuild" )]
static Dictionary<string, List<string>> CleanBuild()
{
try
{
var sw = System.Diagnostics.Stopwatch.StartNew();
EditorUtility.DisplayProgressBar( "WhatUsesThis", "Getting Assets", 0.2f );
var allAssets = AssetDatabase.FindAssets( "" ).Select( x => AssetDatabase.GUIDToAssetPath( x ) ).Distinct().ToArray();
var dependancies = new Dictionary<string, string[]>();
var i = 0;
foreach ( var asset in allAssets )
{
dependancies[asset] = AssetDatabase.GetDependencies( asset, false );
i++;
if ( i%100 == 0 && EditorUtility.DisplayCancelableProgressBar( "WhatUsesThis", $"Getting Dependancies [{i}/{allAssets.Length}]", i / (float)allAssets.Length ) )
return new Dictionary<string, List<string>>();
}
EditorUtility.DisplayProgressBar( "WhatUsesThis", "Building Dependants", 0.9f );
_dict = new Dictionary<string, List<string>>();
foreach ( var d in dependancies )
{
foreach ( var dependant in d.Value )
{
if ( !_dict.TryGetValue( dependant, out var list ) )
{
list = new List<string>();
_dict[dependant] = list;
}
list.Add( d.Key );
}
}
//Debug.Log( $"Generating took {sw.Elapsed.TotalSeconds}s" );
Save();
return _dict;
}
finally
{
EditorUtility.ClearProgressBar();
}
}
static void Save()
{
if ( _dict == null )
return;
var sw = System.Diagnostics.Stopwatch.StartNew();
using ( var stream = new System.IO.FileStream( CacheFilename, System.IO.FileMode.Create ) )
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize( stream, _dict );
}
//Debug.Log( $"Saving took {sw.Elapsed.TotalSeconds}s" );
}
static Dictionary<string, List<string>> Load()
{
try
{
var sw = System.Diagnostics.Stopwatch.StartNew();
using ( var stream = new System.IO.FileStream( CacheFilename, System.IO.FileMode.Open ) )
{
BinaryFormatter bin = new BinaryFormatter();
_dict = (Dictionary<string, List<string>>)bin.Deserialize( stream );
}
//Debug.Log( $"Loading took {sw.Elapsed.TotalSeconds}s" );
}
catch ( System.Exception )
{
_dict = null;
}
return _dict;
}
[MenuItem( "Assets/What uses this?" )]
private static void FindParentAssets()
{
int iCount = 0;
foreach ( var selectedObj in Selection.GetFiltered( typeof( UnityEngine.Object ), SelectionMode.Assets ) )
{
var selected = AssetDatabase.GetAssetPath( selectedObj );
Debug.Log( $"<color=#5C93B9>What uses <b>{selected}</b>?</color>", selectedObj );
if ( Dict.TryGetValue( selected, out var dependants ) )
{
foreach ( var d in dependants )
{
Debug.Log( $"<color=#8CA166> {d}</color>", AssetDatabase.LoadAssetAtPath<Object>( d ) );
iCount++;
}
}
}
Debug.Log( $"<color=#5C93B9>Search complete, found <b>{iCount}</b> result{( iCount == 1 ? "" : "s" )}</color>");
}
[MenuItem( "Assets/What does this use?" )]
private static void FincChildAssets()
{
int iCount = 0;
foreach ( var selectedObj in Selection.GetFiltered( typeof( UnityEngine.Object ), SelectionMode.Assets ) )
{
var selected = AssetDatabase.GetAssetPath( selectedObj );
Debug.Log( $"<color=#5C93B9>What does <b>{selected}</b> use?</color>", selectedObj );
foreach ( var d in AssetDatabase.GetDependencies( selected, false ) )
{
Debug.Log( $"<color=#8CA166> {d}</color>", AssetDatabase.LoadAssetAtPath<Object>( d ) );
iCount++;
}
}
Debug.Log( $"<color=#5C93B9>Search complete, found <b>{iCount}</b> result{( iCount == 1 ? "" : "s" )}</color>" );
}
}
}