Repository navigation
Expand file tree
/
Copy pathReferenceFinder.cs
More file actions
189 lines (158 loc) · 6.14 KB
/
Copy pathReferenceFinder.cs
File metadata and controls
189 lines (158 loc) · 6.14 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
public class ReferenceFinder : EditorWindow
{
private string _targetComponentName;
private List<GameObject> _foundAssets = new List<GameObject>();
private Vector2 _currentScrollPosition;
private static IEnumerable<Type> _cashedComponents;
private static Dictionary<string, List<Type>> _typeDict;
static MonoScript[] _monoScripts;
private static bool _isFirstTime = true;
//開かれ方と、開かれたときの挙動
[MenuItem("ReferenceFinder/Search")]
private static void Open()
{
//開かれる際には一応取得
_cashedComponents = GetAllTypes();
GetWindow<ReferenceFinder>("Components Reference Finder.");
}
private void OnGUI()
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Never change Scene before reset this window");
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("This window can search active scene objects and all prefabs");
EditorGUILayout.EndHorizontal();
//Editorに枠を出して、入力を_targetComponentNameに格納する
EditorGUILayout.BeginHorizontal();
_targetComponentName =
EditorGUILayout.TextField("Target Component Name: ", _targetComponentName);
EditorGUILayout.EndHorizontal();
if (_foundAssets.Count > 0)
{
_currentScrollPosition = EditorGUILayout.BeginScrollView(_currentScrollPosition);
foreach (var asset in _foundAssets)
{
EditorGUILayout.ObjectField(asset.name, asset, typeof(GameObject), false);
}
EditorGUILayout.EndScrollView();
}
if (GUILayout.Button("Search"))
{
_foundAssets.Clear();
var guids = AssetDatabase.FindAssets("t:GameObject", null);
foreach (var guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
var loadAsset = AssetDatabase.LoadAssetAtPath<GameObject>(path);
var typeCash = GetType(_targetComponentName) ?? null;
if (typeCash == null)
{
Debug.Log("No Type Found.");
return;
}
var tmp = loadAsset.GetComponentsInChildren(GetType(_targetComponentName) ?? null);
foreach (var kari in tmp)
{
_foundAssets.Add(kari.gameObject);
}
}
var allObj = GetObjectsInAllScene();
foreach (var obj in allObj)
{
var typeCash = GetType(_targetComponentName) ?? null;
var tmp = obj.GetComponentsInChildren(GetType(_targetComponentName) ?? null);
if (typeCash == null)
{
Debug.Log("No Type Found.");
return;
}
foreach (var kari in tmp)
{
_foundAssets.Add(kari.gameObject);
}
}
}
}
/// <summary>
/// プロジェクト内に存在する全スクリプトファイル
/// </summary>
static MonoScript[] MonoScripts
{
get { return _monoScripts ?? (_monoScripts = Resources.FindObjectsOfTypeAll<MonoScript>().ToArray()); }
}
/// <summary>
/// クラス名からタイプを取得する
/// </summary>
private static Type GetType(string className)
{
if (_typeDict == null)
{
// Dictionary作成
_typeDict = new Dictionary<string, List<Type>>();
foreach (var type in _cashedComponents)
{
if (!_typeDict.ContainsKey(type.Name))
{
_typeDict.Add(type.Name, new List<Type>());
}
_typeDict[type.Name].Add(type);
}
}
//クラスが存在する場合、リストに表示
if (_typeDict.ContainsKey(className))
{
return _typeDict[className][0];
}
else
{
//クラスが存在しない場合、念の為取得、再走
if (_isFirstTime)
{
Debug.Log("Not found. ReScanning...");
_cashedComponents = GetAllTypes();
_isFirstTime = false;
GetType(className);
}
_isFirstTime = true;
return null;
}
}
/// <summary>
/// 全てのクラスタイプを取得
/// </summary>
private static IEnumerable<Type> GetAllTypes()
{
//Unity標準のクラスタイプを取得する
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(asm => asm.GetTypes())
.Where(type => type != null && !string.IsNullOrEmpty(type.Namespace))
.Where(type => type.Namespace.Contains("UnityEngine"));
//自作クラスも取得できるように
var localTypes = MonoScripts
.Where(script => script != null)
.Select(script => script.GetClass())
.Where(classType => classType != null)
.Where(classType => classType.Module.Name == "Assembly-CSharp.dll");
return types.Concat(localTypes).Distinct();
}
private static GameObject[] GetObjectsInAllScene(bool includeInactive = true)
{
var sceneNumber = SceneManager.sceneCount;
// 空の IEnumerable<T>
IEnumerable<GameObject> resultObjects = (GameObject[]) Enumerable.Empty<GameObject>();
for (int i = 0; i < sceneNumber; i++)
{
var obj = SceneManager.GetSceneAt(i).GetRootGameObjects();
resultObjects = resultObjects.Concat(obj);
}
return resultObjects.ToArray();
}
}