forked from networm/ShowAssetRelationship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShowAssetRelationship.cs
121 lines (99 loc) · 3.39 KB
/
ShowAssetRelationship.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
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class ShowAssetRelationship : EditorWindow
{
[MenuItem("Tools/Show Asset Relationship")]
private static void ShowWindow()
{
var window = GetWindow(typeof(ShowAssetRelationship));
window.Show();
}
private void OnGUI()
{
_targetDependent = EditorGUILayout.ObjectField("Dependent: ", _targetDependent, typeof(Object), false);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Find"))
{
_results = Find(AssetDatabase.GetAssetPath(_targetDependent),
AssetDatabase.GetAssetPath(_targetDependency));
if (_results.Count == 0)
{
ShowNotification(new GUIContent("Could not find relationship between two assets"));
}
}
if (GUILayout.Button("Swap"))
{
var temp = _targetDependent;
_targetDependent = _targetDependency;
_targetDependency = temp;
}
if (GUILayout.Button("Clear"))
{
_targetDependent = null;
_targetDependency = null;
_results = null;
}
GUILayout.EndHorizontal();
if (_results != null)
{
GUILayout.BeginHorizontal();
foreach (var result in _results)
{
GUILayout.BeginVertical();
foreach (var asset in result.Routine)
{
EditorGUILayout.ObjectField("", asset, typeof(Object), false);
}
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
}
_targetDependency = EditorGUILayout.ObjectField("Dependeny: ", _targetDependency, typeof(Object), false);
}
private List<Result> Find(string targetDependent, string targetDependency)
{
var stack = new Stack<string>();
stack.Push(targetDependent);
var results = new List<Result>();
var dependencies = AssetDatabase.GetDependencies(targetDependent, false);
foreach (var dependency in dependencies)
{
RecursiveFind(dependency, targetDependency, stack, results);
}
return results;
}
private void RecursiveFind(string targetDependent, string targetDependency, Stack<string> stack,
List<Result> results)
{
stack.Push(targetDependent);
if (targetDependent == targetDependency)
{
var result = new Result(stack);
results.Add(result);
}
else
{
var dependencies = AssetDatabase.GetDependencies(targetDependent, false);
foreach (var dependency in dependencies)
{
RecursiveFind(dependency, targetDependency, stack, results);
}
}
stack.Pop();
}
private class Result
{
public readonly List<Object> Routine = new List<Object>();
public Result(Stack<string> result)
{
foreach (var path in result)
{
Routine.Insert(0, AssetDatabase.LoadMainAssetAtPath(path));
}
}
}
private Object _targetDependent;
private Object _targetDependency;
private List<Result> _results;
}