|
9 | 9 |
|
10 | 10 | internal class DependencyResolver
|
11 | 11 | {
|
12 |
| - private const int NumAssetPropertiesReferencesResolvedPerFrame = 100; |
13 |
| - |
14 |
| - private bool _isResolvingCompleted; |
15 |
| - public bool IsResolvingCompleted |
16 |
| - { |
17 |
| - get { return _isResolvingCompleted; } |
18 |
| - set { _isResolvingCompleted = value; } |
19 |
| - } |
20 |
| - |
21 | 12 | private DependencyViewerGraph _graph;
|
22 | 13 | private DependencyViewerSettings _settings;
|
| 14 | + private DependencyResolver_References _referencesResolver; |
| 15 | + private DependencyResolver_Dependencies _dependenciesResolver; |
23 | 16 |
|
24 | 17 | public DependencyResolver(DependencyViewerGraph graph, DependencyViewerSettings settings)
|
25 | 18 | {
|
26 | 19 | _graph = graph;
|
27 | 20 | _settings = settings;
|
| 21 | + _referencesResolver = new DependencyResolver_References(graph, settings); |
| 22 | + _dependenciesResolver = new DependencyResolver_Dependencies(settings); |
28 | 23 | }
|
29 | 24 |
|
30 | 25 | public IEnumerator<DependencyViewerOperation> BuildGraph()
|
31 | 26 | {
|
32 | 27 | if (_settings.FindDependencies)
|
33 | 28 | {
|
34 |
| - FindDependencies(_graph.RefTargetNode, _settings.DependenciesDepth); |
| 29 | + _dependenciesResolver.FindDependencies(_graph.RefTargetNode, _settings.DependenciesDepth); |
35 | 30 | }
|
36 | 31 |
|
37 |
| - foreach (var currentOperation in FindReferences()) |
| 32 | + foreach (var currentOperation in _referencesResolver.FindReferences()) |
38 | 33 | {
|
39 | 34 | yield return currentOperation;
|
40 | 35 | }
|
41 | 36 | }
|
42 |
| - |
43 |
| - private IEnumerable<DependencyViewerOperation> FindReferences() |
44 |
| - { |
45 |
| - if (_settings.SceneSearchType != DependencyViewerSettings.SceneSearchMode.NoSearch) |
46 |
| - { |
47 |
| - // Search references in scenes |
48 |
| - List<Scene> currentOpenedScenes = DependencyViewerUtility.GetCurrentOpenedScenes(); |
49 |
| - if (_settings.FindReferences) |
50 |
| - { |
51 |
| - foreach (var currentOperation in FindReferencesAmongGameObjects(_graph.RefTargetNode, currentOpenedScenes)) |
52 |
| - { |
53 |
| - yield return currentOperation; |
54 |
| - } |
55 |
| - } |
56 |
| - } |
57 |
| - |
58 |
| - bool searchOnlyInCurrentScene = (_settings.SceneSearchType == DependencyViewerSettings.SceneSearchMode.SearchOnlyInCurrentScene); |
59 |
| - if (_settings.FindReferences && !searchOnlyInCurrentScene) |
60 |
| - { |
61 |
| - // Search references in assets |
62 |
| - foreach (var currentOperation in FindReferencesAmongAssets(_graph.RefTargetNode)) |
63 |
| - { |
64 |
| - yield return currentOperation; |
65 |
| - } |
66 |
| - } |
67 |
| - } |
68 |
| - |
69 |
| - private IEnumerable<DependencyViewerOperation> FindReferencesAmongGameObjects(DependencyViewerNode node, List<Scene> scenes) |
70 |
| - { |
71 |
| - AssetDependencyResolverOperation operationStatus = new AssetDependencyResolverOperation(); |
72 |
| - operationStatus.node = node; |
73 |
| - |
74 |
| - List<GameObject> allGameObjects = GetAllGameObjectsFromScenes(scenes); |
75 |
| - operationStatus.numTotalAssets = allGameObjects.Count; |
76 |
| - |
77 |
| - int numPropertiesCheck = 0; |
78 |
| - for (int i = 0; i < allGameObjects.Count; ++i) |
79 |
| - { |
80 |
| - GameObject currentGo = allGameObjects[i]; |
81 |
| - operationStatus.AssetBeingProcessed = currentGo; |
82 |
| - |
83 |
| - Component[] components = currentGo.GetComponents<Component>(); |
84 |
| - for (int componentIndex = 0; componentIndex < components.Length; ++componentIndex) |
85 |
| - { |
86 |
| - Component component = components[componentIndex]; |
87 |
| - if (component == null) |
88 |
| - { |
89 |
| - continue; |
90 |
| - } |
91 |
| - |
92 |
| - SerializedObject componentSO = new SerializedObject(component); |
93 |
| - SerializedProperty componentSP = componentSO.GetIterator(); |
94 |
| - |
95 |
| - while (componentSP.NextVisible(true)) |
96 |
| - { |
97 |
| - // Reference found! |
98 |
| - if (componentSP.propertyType == SerializedPropertyType.ObjectReference && |
99 |
| - componentSP.objectReferenceValue == node.TargetObject && |
100 |
| - IsObjectAllowedBySettings(component)) |
101 |
| - { |
102 |
| - DependencyViewerNode referenceNode = new DependencyViewerNode(component); |
103 |
| - DependencyViewerGraph.CreateNodeLink(referenceNode, node); |
104 |
| - } |
105 |
| - |
106 |
| - ++numPropertiesCheck; |
107 |
| - if (numPropertiesCheck > NumAssetPropertiesReferencesResolvedPerFrame) |
108 |
| - { |
109 |
| - numPropertiesCheck = 0; |
110 |
| - yield return operationStatus; |
111 |
| - } |
112 |
| - } |
113 |
| - } |
114 |
| - |
115 |
| - ++operationStatus.numProcessedAssets; |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 |
| - private List<GameObject> GetAllGameObjectsFromScenes(List<Scene> scenes) |
120 |
| - { |
121 |
| - List<GameObject> gameObjects = new List<GameObject>(); |
122 |
| - List<GameObject> gameObjectsToCheck = new List<GameObject>(); |
123 |
| - |
124 |
| - List<GameObject> rootGameObjects = new List<GameObject>(); |
125 |
| - for (int sceneIdx = 0; sceneIdx < scenes.Count; ++sceneIdx) |
126 |
| - { |
127 |
| - Scene scene = scenes[sceneIdx]; |
128 |
| - scene.GetRootGameObjects(rootGameObjects); |
129 |
| - gameObjectsToCheck.AddRange(rootGameObjects); |
130 |
| - } |
131 |
| - |
132 |
| - for (int gameObjectsToCheckIdx = 0; gameObjectsToCheckIdx < gameObjectsToCheck.Count; ++gameObjectsToCheckIdx) |
133 |
| - { |
134 |
| - GameObject currentGo = gameObjectsToCheck[gameObjectsToCheckIdx]; |
135 |
| - for (int childIdx = 0; childIdx < currentGo.transform.childCount; ++childIdx) |
136 |
| - { |
137 |
| - gameObjectsToCheck.Add(currentGo.transform.GetChild(childIdx).gameObject); |
138 |
| - } |
139 |
| - gameObjects.Add(currentGo); |
140 |
| - } |
141 |
| - |
142 |
| - return gameObjects; |
143 |
| - } |
144 |
| - |
145 |
| - private IEnumerable<DependencyViewerOperation> FindReferencesAmongAssets(DependencyViewerNode node) |
146 |
| - { |
147 |
| - string[] excludeFilters = _settings.ExcludeAssetFilters.Split(','); |
148 |
| - int numPropertyChecked = 0; |
149 |
| - |
150 |
| - var allLocalAssetPaths = from assetPath in AssetDatabase.GetAllAssetPaths() |
151 |
| - where assetPath.StartsWith("Assets/") && !IsAssetPathExcluded(assetPath, ref excludeFilters) |
152 |
| - select assetPath; |
153 |
| - |
154 |
| - AssetDependencyResolverOperation operationStatus = new AssetDependencyResolverOperation |
155 |
| - { |
156 |
| - node = node, |
157 |
| - numTotalAssets = allLocalAssetPaths.Count() |
158 |
| - }; |
159 |
| - |
160 |
| - foreach (string assetPath in allLocalAssetPaths) |
161 |
| - { |
162 |
| - ++operationStatus.numProcessedAssets; |
163 |
| - |
164 |
| - UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath); |
165 |
| - if (obj != null) |
166 |
| - { |
167 |
| - SerializedObject objSO = new SerializedObject(obj); |
168 |
| - SerializedProperty sp = objSO.GetIterator(); |
169 |
| - while (sp.NextVisible(true)) |
170 |
| - { |
171 |
| - if (sp.propertyType == SerializedPropertyType.ObjectReference && |
172 |
| - sp.objectReferenceValue == node.TargetObject && |
173 |
| - IsObjectAllowedBySettings(sp.objectReferenceValue)) |
174 |
| - { |
175 |
| - // Reference found! |
176 |
| - DependencyViewerNode reference = new DependencyViewerNode(obj); |
177 |
| - DependencyViewerGraph.CreateNodeLink(reference, node); |
178 |
| - } |
179 |
| - |
180 |
| - ++numPropertyChecked; |
181 |
| - |
182 |
| - if (numPropertyChecked > NumAssetPropertiesReferencesResolvedPerFrame) |
183 |
| - { |
184 |
| - operationStatus.AssetBeingProcessed = obj; |
185 |
| - |
186 |
| - numPropertyChecked = 0; |
187 |
| - yield return operationStatus; |
188 |
| - } |
189 |
| - } |
190 |
| - } |
191 |
| - } |
192 |
| - } |
193 |
| - |
194 |
| - private bool IsAssetPathExcluded(string assetPath, ref string[] excludeFilters) |
195 |
| - { |
196 |
| - for (int i = 0; i < excludeFilters.Length; ++i) |
197 |
| - { |
198 |
| - if (assetPath.EndsWith(excludeFilters[i])) |
199 |
| - { |
200 |
| - return true; |
201 |
| - } |
202 |
| - } |
203 |
| - |
204 |
| - if (_settings.ReferencesAssetDirectories != null && |
205 |
| - _settings.ReferencesAssetDirectories.Length > 0) |
206 |
| - { |
207 |
| - bool isAssetAmongReferencesDirectory = false; |
208 |
| - string assetFullPath = Path.GetFullPath(assetPath); |
209 |
| - for (int i = 0; i < _settings.ReferencesAssetDirectories.Length; ++i) |
210 |
| - { |
211 |
| - if (Directory.Exists(_settings.ReferencesAssetDirectories[i])) |
212 |
| - { |
213 |
| - string referenceAssetFullPath = Path.GetFullPath(_settings.ReferencesAssetDirectories[i]); |
214 |
| - if (assetFullPath.StartsWith(referenceAssetFullPath)) |
215 |
| - { |
216 |
| - isAssetAmongReferencesDirectory = true; |
217 |
| - break; |
218 |
| - } |
219 |
| - } |
220 |
| - } |
221 |
| - |
222 |
| - return !isAssetAmongReferencesDirectory; |
223 |
| - } |
224 |
| - |
225 |
| - return false; |
226 |
| - } |
227 |
| - |
228 |
| - private void FindReferenceInGameObject(DependencyViewerNode node, GameObject rootGameObject, int depth = 1) |
229 |
| - { |
230 |
| - Component[] components = rootGameObject.GetComponents<MonoBehaviour>(); |
231 |
| - for (int componentsIdx = 0; componentsIdx < components.Length; ++componentsIdx) |
232 |
| - { |
233 |
| - Component component = components[componentsIdx]; |
234 |
| - SerializedObject so = new SerializedObject(component); |
235 |
| - SerializedProperty sp = so.GetIterator(); |
236 |
| - while (sp.NextVisible(true)) |
237 |
| - { |
238 |
| - if (sp.propertyType == SerializedPropertyType.ObjectReference && |
239 |
| - sp.objectReferenceValue == node.TargetObject && |
240 |
| - IsObjectAllowedBySettings(sp.objectReferenceValue)) |
241 |
| - { |
242 |
| - // Reference found! |
243 |
| - DependencyViewerNode reference = new DependencyViewerNode(component); |
244 |
| - DependencyViewerGraph.CreateNodeLink(reference, node); |
245 |
| - } |
246 |
| - } |
247 |
| - } |
248 |
| - } |
249 |
| - |
250 |
| - private void FindDependencies(DependencyViewerNode node, int depth = 1) |
251 |
| - { |
252 |
| - if (node.TargetObject is GameObject) |
253 |
| - { |
254 |
| - GameObject targetGameObject = node.TargetObject as GameObject; |
255 |
| - Component[] components = targetGameObject.GetComponents<Component>(); |
256 |
| - for (int i = 0; i < components.Length; ++i) |
257 |
| - { |
258 |
| - FindDependencies(node, components[i], depth); |
259 |
| - } |
260 |
| - |
261 |
| - if (DependencyResolverUtility.IsPrefab(node.TargetObject)) |
262 |
| - { |
263 |
| - UDGV.GameObjectUtility.ForeachChildrenGameObject(targetGameObject, (childGo) => |
264 |
| - { |
265 |
| - bool isPrefabChild = true; |
266 |
| - components = childGo.GetComponents<Component>(); |
267 |
| - for (int i = 0; i < components.Length; ++i) |
268 |
| - { |
269 |
| - FindDependencies(node, components[i], depth, isPrefabChild); |
270 |
| - } |
271 |
| - }); |
272 |
| - } |
273 |
| - } |
274 |
| - else |
275 |
| - { |
276 |
| - FindDependencies(node, node.TargetObject, depth); |
277 |
| - } |
278 |
| - } |
279 |
| - |
280 |
| - private void FindDependencies(DependencyViewerNode node, UnityEngine.Object obj, int depth = 1, bool isPrefabChild = false) |
281 |
| - { |
282 |
| - SerializedObject targetObjectSO = new SerializedObject(obj); |
283 |
| - SerializedProperty sp = targetObjectSO.GetIterator(); |
284 |
| - while (sp.NextVisible(true)) |
285 |
| - { |
286 |
| - if (sp.propertyType == SerializedPropertyType.ObjectReference && |
287 |
| - sp.objectReferenceValue != null && |
288 |
| - IsObjectAllowedBySettings(sp.objectReferenceValue)) |
289 |
| - { |
290 |
| - // Dependency found! |
291 |
| - DependencyViewerNode dependencyNode = new DependencyViewerNode(sp.objectReferenceValue); |
292 |
| - DependencyViewerGraph.CreateNodeLink(node, dependencyNode); |
293 |
| - if (isPrefabChild) |
294 |
| - { |
295 |
| - Component comp = obj as Component; |
296 |
| - dependencyNode.GameObjectNameAsPrefabChild = comp.gameObject.name; |
297 |
| - } |
298 |
| - |
299 |
| - if (depth > 1) |
300 |
| - { |
301 |
| - FindDependencies(dependencyNode, sp.objectReferenceValue, depth - 1); |
302 |
| - } |
303 |
| - } |
304 |
| - } |
305 |
| - } |
306 |
| - |
307 |
| - private bool IsObjectAllowedBySettings(UnityEngine.Object obj) |
308 |
| - { |
309 |
| - return (_settings.CanObjectTypeBeIncluded(obj)); |
310 |
| - } |
311 | 37 | }
|
0 commit comments