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

Commit 5e5559c

Browse files
committed
Assets that have references on prefabs should now be found
1 parent b5dc552 commit 5e5559c

File tree

5 files changed

+118
-29
lines changed

5 files changed

+118
-29
lines changed

Editor/Operations/AssetDependencyResolverOperation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ internal class AssetDependencyResolverOperation : DependencyViewerOperation
77
{
88
public int numTotalAssets { get; set; }
99
public int numProcessedAssets { get; set; }
10+
public int numProcessedProperties { get; set; }
1011
public DependencyViewerNode node { get; set; }
1112

1213
public UnityEngine.Object AssetBeingProcessed { get; set; }

Editor/Resolver/DependencyResolver_Dependencies.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ public void FindDependencies(DependencyViewerNode node, int depth = 1)
2727
{
2828
UDGV.GameObjectUtility.ForeachChildrenGameObject(targetGameObject, (childGo) =>
2929
{
30-
bool isPrefabChild = true;
3130
components = childGo.GetComponents<Component>();
3231
for (int i = 0; i < components.Length; ++i)
3332
{
34-
FindDependencies(node, components[i], depth, isPrefabChild);
33+
FindDependencies(node, components[i], depth, targetGameObject);
3534
}
3635
});
3736
}
@@ -42,7 +41,7 @@ public void FindDependencies(DependencyViewerNode node, int depth = 1)
4241
}
4342
}
4443

45-
private void FindDependencies(DependencyViewerNode node, UnityEngine.Object obj, int depth = 1, bool isPrefabChild = false)
44+
private void FindDependencies(DependencyViewerNode node, UnityEngine.Object obj, int depth = 1, GameObject prefabRoot = null)
4645
{
4746
SerializedObject targetObjectSO = new SerializedObject(obj);
4847
SerializedProperty sp = targetObjectSO.GetIterator();
@@ -55,10 +54,10 @@ private void FindDependencies(DependencyViewerNode node, UnityEngine.Object obj,
5554
// Dependency found!
5655
DependencyViewerNode dependencyNode = new DependencyViewerNode(sp.objectReferenceValue);
5756
DependencyViewerGraph.CreateNodeLink(node, dependencyNode);
58-
if (isPrefabChild)
57+
if (prefabRoot != null)
5958
{
6059
Component comp = obj as Component;
61-
dependencyNode.GameObjectNameAsPrefabChild = comp.gameObject.name;
60+
dependencyNode.SetAsPrefabContainerInfo(prefabRoot, comp.gameObject.name);
6261
}
6362

6463
if (depth > 1)

Editor/Resolver/DependencyResolver_References.cs

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ private IEnumerable<DependencyViewerOperation> FindReferencesAmongGameObjects(De
9898
private IEnumerable<DependencyViewerOperation> FindReferencesAmongAssets(DependencyViewerNode node)
9999
{
100100
string[] excludeFilters = _settings.ExcludeAssetFilters.Split(',');
101-
int numPropertyChecked = 0;
102101

103102
var allLocalAssetPaths = from assetPath in AssetDatabase.GetAllAssetPaths()
104103
where assetPath.StartsWith("Assets/") && !IsAssetPathExcluded(assetPath, ref excludeFilters)
@@ -117,33 +116,94 @@ where assetPath.StartsWith("Assets/") && !IsAssetPathExcluded(assetPath, ref exc
117116
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetPath);
118117
if (obj != null)
119118
{
120-
SerializedObject objSO = new SerializedObject(obj);
121-
SerializedProperty sp = objSO.GetIterator();
122-
while (sp.NextVisible(true))
119+
bool isPrefab = (obj is GameObject);
120+
if (isPrefab)
123121
{
124-
if (sp.propertyType == SerializedPropertyType.ObjectReference &&
125-
sp.objectReferenceValue == node.TargetObject &&
126-
IsObjectAllowedBySettings(sp.objectReferenceValue))
122+
GameObject prefab = obj as GameObject;
123+
foreach (var op in FindReferencesAmongPrefabChildren(node, prefab, operationStatus, prefab))
127124
{
128-
// Reference found!
129-
DependencyViewerNode reference = new DependencyViewerNode(obj);
130-
DependencyViewerGraph.CreateNodeLink(reference, node);
125+
yield return op;
131126
}
132-
133-
++numPropertyChecked;
134-
135-
if (numPropertyChecked > NumAssetPropertiesReferencesResolvedPerFrame)
127+
}
128+
else
129+
{
130+
foreach (var op in FindReferencesOnUnityObject(node, obj, operationStatus))
136131
{
137-
operationStatus.AssetBeingProcessed = obj;
138-
139-
numPropertyChecked = 0;
140-
yield return operationStatus;
132+
yield return op;
141133
}
142134
}
143135
}
144136
}
145137
}
146138

139+
private IEnumerable<DependencyViewerOperation> FindReferencesOnUnityObject(
140+
DependencyViewerNode node,
141+
UnityEngine.Object obj,
142+
AssetDependencyResolverOperation op,
143+
GameObject prefabRoot = null)
144+
{
145+
SerializedObject objSO = new SerializedObject(obj);
146+
SerializedProperty sp = objSO.GetIterator();
147+
while (sp.NextVisible(true))
148+
{
149+
if (IsPropertyADependency(sp, node))
150+
{
151+
// Reference found!
152+
DependencyViewerNode reference = new DependencyViewerNode(obj);
153+
DependencyViewerGraph.CreateNodeLink(reference, node);
154+
if (prefabRoot != null)
155+
{
156+
reference.SetAsPrefabContainerInfo(prefabRoot, (obj as Component).gameObject.name);
157+
}
158+
}
159+
160+
++op.numProcessedProperties;
161+
162+
if (op.numProcessedProperties > NumAssetPropertiesReferencesResolvedPerFrame)
163+
{
164+
op.AssetBeingProcessed = obj;
165+
166+
op.numProcessedProperties = 0;
167+
yield return op;
168+
}
169+
}
170+
}
171+
172+
private IEnumerable<DependencyViewerOperation> FindReferencesAmongPrefabChildren(
173+
DependencyViewerNode node,
174+
GameObject gameObject,
175+
AssetDependencyResolverOperation op,
176+
GameObject prefabRoot)
177+
{
178+
// Find references among the components of the GameObject...
179+
Component[] components = gameObject.GetComponents<Component>();
180+
for (int i = 0; i < components.Length; ++i)
181+
{
182+
foreach (var operation in FindReferencesOnUnityObject(node, components[i], op, prefabRoot))
183+
{
184+
yield return operation;
185+
}
186+
}
187+
188+
// ...then make same thing on children
189+
Transform trans = gameObject.transform;
190+
for (int i = 0; i < trans.childCount; ++i)
191+
{
192+
GameObject child = trans.GetChild(i).gameObject;
193+
foreach (var operation in FindReferencesAmongPrefabChildren(node, child, op, prefabRoot))
194+
{
195+
yield return operation;
196+
}
197+
}
198+
}
199+
200+
private bool IsPropertyADependency(SerializedProperty sp, DependencyViewerNode node)
201+
{
202+
return sp.propertyType == SerializedPropertyType.ObjectReference &&
203+
sp.objectReferenceValue == node.TargetObject &&
204+
IsObjectAllowedBySettings(sp.objectReferenceValue);
205+
}
206+
147207
private List<GameObject> GetAllGameObjectsFromScenes(List<Scene> scenes)
148208
{
149209
List<GameObject> gameObjects = new List<GameObject>();

Editor/Viewer/DependencyViewerGraphDrawer.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,29 @@ private void DrawNode(DependencyViewerNode node)
113113

114114
GUILayout.BeginArea(boxInsideRect);
115115
{
116-
EditorGUILayout.ObjectField(node.TargetObject, node.TargetObject.GetType(), false);
116+
bool allowSceneObjects = false;
117+
EditorGUILayout.ObjectField(node.TargetObject, node.TargetObject.GetType(), allowSceneObjects);
118+
119+
if (node.PrefabContainer != null)
120+
{
121+
DrawPrefabContainer(node);
122+
}
117123
}
118124
GUILayout.EndArea();
119125
}
120126

127+
private void DrawPrefabContainer(DependencyViewerNode node)
128+
{
129+
bool allowSceneObjects = false;
130+
EditorGUILayout.BeginHorizontal();
131+
{
132+
GUIContent label = new GUIContent("Prefab", $"Prefab reference, on GameObject named '{node.GameObjectNameAsPrefabChild}'");
133+
EditorGUILayout.LabelField(label, GUILayout.Width(40));
134+
EditorGUILayout.ObjectField(node.PrefabContainer, typeof(GameObject), allowSceneObjects);
135+
}
136+
EditorGUILayout.EndHorizontal();
137+
}
138+
121139
private void DrawNodeTitleBar(DependencyViewerNode node, Rect boxRect)
122140
{
123141
Rect boxTitleRect =

Editor/Viewer/DependencyViewerNode.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using UnityEditor;
45
using UnityEngine;
56

67
internal class DependencyViewerNode
@@ -16,10 +17,9 @@ public string Name
1617
return "(null)";
1718
}
1819

19-
string prefix = IsPrefabChild ? $"[{GameObjectNameAsPrefabChild}] " : string.Empty;
2020
string suffix = (_targetObject is UnityEditor.MonoScript) ? " (Script)" : string.Empty;
2121

22-
return $"{prefix}{_targetObject.name}{suffix}";
22+
return $"{_targetObject.name}{suffix}";
2323
}
2424
}
2525

@@ -30,8 +30,8 @@ public UnityEngine.Object TargetObject
3030
private set { _targetObject = value; }
3131
}
3232

33-
public string GameObjectNameAsPrefabChild { get; set; }
34-
public bool IsPrefabChild => !string.IsNullOrEmpty(GameObjectNameAsPrefabChild);
33+
public string GameObjectNameAsPrefabChild { get; private set; }
34+
public GameObject PrefabContainer { get; private set; }
3535

3636
private Vector2 _position;
3737
public Vector2 Position
@@ -69,9 +69,20 @@ public DependencyViewerNode(UnityEngine.Object targetObject)
6969
_rightInputs = new List<DependencyViewerNode>();
7070
}
7171

72+
public void SetAsPrefabContainerInfo(GameObject prefabContainer, string gameObjectName)
73+
{
74+
PrefabContainer = prefabContainer;
75+
GameObjectNameAsPrefabChild = gameObjectName;
76+
}
77+
7278
public float GetHeight()
7379
{
74-
return DependencyViewerGraphDrawer.NodeHeight;
80+
int numAdditionalLines = 0;
81+
if (PrefabContainer != null)
82+
{
83+
++numAdditionalLines;
84+
}
85+
return DependencyViewerGraphDrawer.NodeHeight + numAdditionalLines * EditorGUIUtility.singleLineHeight;
7586
}
7687

7788
public float GetWidth()

0 commit comments

Comments
 (0)