Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Commit b5dc552

Browse files
committed
Split the DependencyResolver in 2 classes "DependencyResolver_Dependencies" and "DependencyResolver_References"
1 parent 0feee25 commit b5dc552

6 files changed

+316
-281
lines changed

Editor/Resolver/DependencyResolver.cs

Lines changed: 6 additions & 280 deletions
Original file line numberDiff line numberDiff line change
@@ -9,303 +9,29 @@
99

1010
internal class DependencyResolver
1111
{
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-
2112
private DependencyViewerGraph _graph;
2213
private DependencyViewerSettings _settings;
14+
private DependencyResolver_References _referencesResolver;
15+
private DependencyResolver_Dependencies _dependenciesResolver;
2316

2417
public DependencyResolver(DependencyViewerGraph graph, DependencyViewerSettings settings)
2518
{
2619
_graph = graph;
2720
_settings = settings;
21+
_referencesResolver = new DependencyResolver_References(graph, settings);
22+
_dependenciesResolver = new DependencyResolver_Dependencies(settings);
2823
}
2924

3025
public IEnumerator<DependencyViewerOperation> BuildGraph()
3126
{
3227
if (_settings.FindDependencies)
3328
{
34-
FindDependencies(_graph.RefTargetNode, _settings.DependenciesDepth);
29+
_dependenciesResolver.FindDependencies(_graph.RefTargetNode, _settings.DependenciesDepth);
3530
}
3631

37-
foreach (var currentOperation in FindReferences())
32+
foreach (var currentOperation in _referencesResolver.FindReferences())
3833
{
3934
yield return currentOperation;
4035
}
4136
}
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-
}
31137
}

Editor/Resolver/DependencyResolverUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using UnityEditor;
44
using UnityEngine;
55

6-
public static class DependencyResolverUtility
6+
internal static class DependencyResolverUtility
77
{
88
public static bool IsObjectAnAsset(UnityEngine.Object obj)
99
{
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
internal class DependencyResolver_Dependencies
7+
{
8+
private DependencyViewerSettings _settings;
9+
10+
public DependencyResolver_Dependencies(DependencyViewerSettings settings)
11+
{
12+
_settings = settings;
13+
}
14+
15+
public void FindDependencies(DependencyViewerNode node, int depth = 1)
16+
{
17+
if (node.TargetObject is GameObject)
18+
{
19+
GameObject targetGameObject = node.TargetObject as GameObject;
20+
Component[] components = targetGameObject.GetComponents<Component>();
21+
for (int i = 0; i < components.Length; ++i)
22+
{
23+
FindDependencies(node, components[i], depth);
24+
}
25+
26+
if (DependencyResolverUtility.IsPrefab(node.TargetObject))
27+
{
28+
UDGV.GameObjectUtility.ForeachChildrenGameObject(targetGameObject, (childGo) =>
29+
{
30+
bool isPrefabChild = true;
31+
components = childGo.GetComponents<Component>();
32+
for (int i = 0; i < components.Length; ++i)
33+
{
34+
FindDependencies(node, components[i], depth, isPrefabChild);
35+
}
36+
});
37+
}
38+
}
39+
else
40+
{
41+
FindDependencies(node, node.TargetObject, depth);
42+
}
43+
}
44+
45+
private void FindDependencies(DependencyViewerNode node, UnityEngine.Object obj, int depth = 1, bool isPrefabChild = false)
46+
{
47+
SerializedObject targetObjectSO = new SerializedObject(obj);
48+
SerializedProperty sp = targetObjectSO.GetIterator();
49+
while (sp.NextVisible(true))
50+
{
51+
if (sp.propertyType == SerializedPropertyType.ObjectReference &&
52+
sp.objectReferenceValue != null &&
53+
IsObjectAllowedBySettings(sp.objectReferenceValue))
54+
{
55+
// Dependency found!
56+
DependencyViewerNode dependencyNode = new DependencyViewerNode(sp.objectReferenceValue);
57+
DependencyViewerGraph.CreateNodeLink(node, dependencyNode);
58+
if (isPrefabChild)
59+
{
60+
Component comp = obj as Component;
61+
dependencyNode.GameObjectNameAsPrefabChild = comp.gameObject.name;
62+
}
63+
64+
if (depth > 1)
65+
{
66+
FindDependencies(dependencyNode, sp.objectReferenceValue, depth - 1);
67+
}
68+
}
69+
}
70+
}
71+
72+
private bool IsObjectAllowedBySettings(UnityEngine.Object obj)
73+
{
74+
return (_settings.CanObjectTypeBeIncluded(obj));
75+
}
76+
}

0 commit comments

Comments
 (0)