Skip to content

Commit e80ad3b

Browse files
committed
Fixed StackOverflowException that may occurr if there is a huge chain of linked GameObjects in the scene (fixed #45)
1 parent 0342ab2 commit e80ad3b

1 file changed

Lines changed: 8 additions & 13 deletions

File tree

Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class Parameters
3737
public bool dontSearchInSourceAssets = true;
3838
public bool searchInProjectSettings = true;
3939

40-
public int searchDepthLimit = 4;
40+
public int searchDepthLimit = 32;
4141

4242
public bool searchUnusedMaterialProperties = true;
4343

@@ -84,7 +84,6 @@ public class Parameters
8484
private readonly List<object> callStack = new List<object>( 64 );
8585

8686
private Object currentSearchedObject;
87-
private int currentDepth;
8887

8988
private bool searchingSourceAssets;
9089
private bool isInPlayMode;
@@ -160,7 +159,6 @@ public SearchResult Run( Parameters searchParameters )
160159
searchResult = new List<SearchResultGroup>(); // Overall search results
161160

162161
currentSearchedObject = null;
163-
currentDepth = 0;
164162
searchedObjectsCount = 0;
165163
searchStartTime = EditorApplication.timeSinceStartup;
166164

@@ -1002,6 +1000,10 @@ private ReferenceNode SearchObject( object obj )
10021000
return cachedResult;
10031001
}
10041002

1003+
// Comply with the recursive search limit
1004+
if (callStack.Count >= searchParameters.searchDepthLimit)
1005+
return null;
1006+
10051007
searchedObjectsCount++;
10061008

10071009
ReferenceNode result;
@@ -1063,17 +1065,11 @@ private ReferenceNode SearchObject( object obj )
10631065
}
10641066
else
10651067
{
1066-
// Comply with the recursive search limit
1067-
if( currentDepth >= searchParameters.searchDepthLimit )
1068-
return null;
1069-
10701068
callStack.Add( obj );
1071-
currentDepth++;
10721069

10731070
result = PopReferenceNode( obj );
10741071
SearchVariablesWithReflection( result );
10751072

1076-
currentDepth--;
10771073
callStack.RemoveAt( callStack.Count - 1 );
10781074
}
10791075

@@ -1083,10 +1079,9 @@ private ReferenceNode SearchObject( object obj )
10831079
result = null;
10841080
}
10851081

1086-
// Cache the search result if we are skimming through a class (not a struct; i.e. objHash != null)
1087-
// and if the object is a UnityEngine.Object (if not, cache the result only if we have actually found something
1088-
// or we are at the root of the search; i.e. currentDepth == 0)
1089-
if (!(obj is ValueType) && (result != null || unityObject != null || currentDepth == 0))
1082+
// Cache the search result if we are skimming through a class (not a struct) and if the object is a UnityEngine.Object (if not,
1083+
// cache the result only if we have actually found something or we are at the root of the search; i.e. callStack.Count == 0)
1084+
if (obj is not ValueType && (result != null || unityObject != null || callStack.Count == 0))
10901085
{
10911086
if (!searchingSourceAsset)
10921087
{

0 commit comments

Comments
 (0)