From e80ad3b905086803409c80e7df5fa74c0e369809 Mon Sep 17 00:00:00 2001 From: yasirkula Date: Sun, 28 Dec 2025 23:59:50 +0300 Subject: [PATCH] Fixed StackOverflowException that may occurr if there is a huge chain of linked GameObjects in the scene (fixed #45) --- .../Editor/AssetUsageDetector.cs | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs index 79cd69e..f651b57 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs @@ -37,7 +37,7 @@ namespace AssetUsageDetectorNamespace public bool dontSearchInSourceAssets = true; public bool searchInProjectSettings = true; - public int searchDepthLimit = 4; + public int searchDepthLimit = 32; public bool searchUnusedMaterialProperties = true; @@ -84,7 +84,6 @@ namespace AssetUsageDetectorNamespace private readonly List callStack = new List( 64 ); private Object currentSearchedObject; - private int currentDepth; private bool searchingSourceAssets; private bool isInPlayMode; @@ -160,7 +159,6 @@ namespace AssetUsageDetectorNamespace searchResult = new List(); // Overall search results currentSearchedObject = null; - currentDepth = 0; searchedObjectsCount = 0; searchStartTime = EditorApplication.timeSinceStartup; @@ -1002,6 +1000,10 @@ namespace AssetUsageDetectorNamespace return cachedResult; } + // Comply with the recursive search limit + if (callStack.Count >= searchParameters.searchDepthLimit) + return null; + searchedObjectsCount++; ReferenceNode result; @@ -1063,17 +1065,11 @@ namespace AssetUsageDetectorNamespace } else { - // Comply with the recursive search limit - if( currentDepth >= searchParameters.searchDepthLimit ) - return null; - callStack.Add( obj ); - currentDepth++; result = PopReferenceNode( obj ); SearchVariablesWithReflection( result ); - currentDepth--; callStack.RemoveAt( callStack.Count - 1 ); } @@ -1083,10 +1079,9 @@ namespace AssetUsageDetectorNamespace result = null; } - // Cache the search result if we are skimming through a class (not a struct; i.e. objHash != null) - // and if the object is a UnityEngine.Object (if not, cache the result only if we have actually found something - // or we are at the root of the search; i.e. currentDepth == 0) - if (!(obj is ValueType) && (result != null || unityObject != null || currentDepth == 0)) + // Cache the search result if we are skimming through a class (not a struct) and if the object is a UnityEngine.Object (if not, + // cache the result only if we have actually found something or we are at the root of the search; i.e. callStack.Count == 0) + if (obj is not ValueType && (result != null || unityObject != null || callStack.Count == 0)) { if (!searchingSourceAsset) {