diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs index cb5e3da..a66a81d 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs @@ -49,7 +49,7 @@ namespace AssetUsageDetectorNamespace } #endregion - // A set that contains the searched asset(s) and their sub-assets (if any) + // A set that contains the searched scene object(s), asset(s) and their sub-assets (if any) private readonly HashSet objectsToSearchSet = new HashSet(); // Scenes of scene object(s) in objectsToSearchSet private readonly HashSet sceneObjectsToSearchScenesSet = new HashSet(); @@ -610,7 +610,7 @@ namespace AssetUsageDetectorNamespace private void InitializeSearchResultNodes( List searchResult ) { for( int i = 0; i < searchResult.Count; i++ ) - searchResult[i].InitializeNodes( GetReferenceNode ); + searchResult[i].InitializeNodes( GetReferenceNode, objectsToSearchSet ); // If there are any empty groups after node initialization, remove those groups for( int i = searchResult.Count - 1; i >= 0; i-- ) diff --git a/Plugins/AssetUsageDetector/Editor/SearchResult.cs b/Plugins/AssetUsageDetector/Editor/SearchResult.cs index 04170a8..7bef4fd 100644 --- a/Plugins/AssetUsageDetector/Editor/SearchResult.cs +++ b/Plugins/AssetUsageDetector/Editor/SearchResult.cs @@ -515,7 +515,7 @@ namespace AssetUsageDetectorNamespace } // Initializes commonly used variables of the nodes - public void InitializeNodes( Func nodeGetter ) + public void InitializeNodes( Func nodeGetter, HashSet objectsToSearchSet ) { // Remove root nodes that don't have any outgoing links or have null node objects (somehow) for( int i = references.Count - 1; i >= 0; i-- ) @@ -544,6 +544,26 @@ namespace AssetUsageDetectorNamespace List callStack = new List( 8 ); + // If this SearchResultGroup belongs to a scene, simplify the root nodes so that they go directly to either the searched objects or to an asset in a single link + if( Type == GroupType.Scene || Type == GroupType.DontDestroyOnLoad ) + { + Scene scene = references[0].nodeObject is GameObject ? ( (GameObject) references[0].nodeObject ).scene : ( (Component) references[0].nodeObject ).gameObject.scene; + HashSet leafNodes = new HashSet(); + for( int i = references.Count - 1; i >= 0; i-- ) + { + references[i].GetLeafSceneNodesRecursive( scene, leafNodes, references[i], objectsToSearchSet, callStack ); + + if( leafNodes.Count > 0 ) + { + references.RemoveAt( i ); + foreach( ReferenceNode leafNode in leafNodes ) + references.Add( leafNode ); + + leafNodes.Clear(); + } + } + } + // For simplicity's sake, get rid of root nodes that are already part of another node's hierarchy for( int i = references.Count - 1; i >= 0; i-- ) { @@ -1010,12 +1030,59 @@ namespace AssetUsageDetectorNamespace description = "<>"; } - nodeObject = null; // don't hold Object reference, allow Unity to GC used memory + nodeObject = null; // Don't hold Object reference, allow Unity to GC used memory for( int i = 0; i < links.Count; i++ ) links[i].targetNode.InitializeRecursively(); } + // Finds all leaf nodes that originate from this node and have GameObject/Component node objects in the specified scene + public void GetLeafSceneNodesRecursive( Scene scene, HashSet leafNodes, ReferenceNode latestSceneObjectNode, HashSet objectsToSearchSet, List callStack ) + { + if( links.Count == 0 || callStack.ContainsFast( this ) || ( nodeObject is Object && objectsToSearchSet.Contains( (Object) nodeObject ) ) ) // This is a searched object's node + { + leafNodes.Add( latestSceneObjectNode ); + return; + } + + Object unityObject = nodeObject as Object; + if( unityObject != null ) + { + if( AssetDatabase.Contains( unityObject ) ) + { + leafNodes.Add( latestSceneObjectNode ); + return; + } + else if( unityObject is Component ) + { + if( ( (Component) unityObject ).gameObject.scene == scene ) + latestSceneObjectNode = this; + else + { + leafNodes.Add( latestSceneObjectNode ); + return; + } + } + else if( unityObject is GameObject ) + { + if( ( (GameObject) unityObject ).scene == scene ) + latestSceneObjectNode = this; + else + { + leafNodes.Add( latestSceneObjectNode ); + return; + } + } + } + + callStack.Add( this ); + + for( int i = 0; i < links.Count; i++ ) + links[i].targetNode.GetLeafSceneNodesRecursive( scene, leafNodes, latestSceneObjectNode, objectsToSearchSet, callStack ); + + callStack.RemoveAt( callStack.Count - 1 ); + } + // Returns whether or not specified node is part of this node's siblings public bool NodeExistsInChildrenRecursive( ReferenceNode node, List callStack ) { diff --git a/package.json b/package.json index 221d91d..33056e6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.yasirkula.assetusagedetector", "displayName": "Asset Usage Detector", - "version": "1.9.10", + "version": "2.0.0", "documentationUrl": "https://github.com/yasirkula/UnityAssetUsageDetector", "changelogUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/releases", "licensesUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/blob/master/LICENSE.txt",