diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs index 47fed7d..d2341a0 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs @@ -10,6 +10,14 @@ using System.Reflection; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO; +#if UNITY_2017_1_OR_NEWER +using UnityEngine.U2D; +using UnityEditor.U2D; +using UnityEngine.Playables; +#endif +#if UNITY_2017_2_OR_NEWER +using UnityEngine.Tilemaps; +#endif using Object = UnityEngine.Object; namespace AssetUsageDetectorNamespace @@ -108,6 +116,7 @@ namespace AssetUsageDetectorNamespace private int searchDepthLimit; // Depth limit for recursively searching variables of objects + private Object currentObject; private int currentDepth; private BindingFlags fieldModifiers, propertyModifiers; @@ -125,9 +134,11 @@ namespace AssetUsageDetectorNamespace if( searchParameters.objectsToSearch == null ) { Debug.LogError( "objectsToSearch list is empty" ); - return new SearchResult( null, null ); + return new SearchResult( false, null, null ); } + List searchResult = null; + // Get the scenes that are open right now SceneSetup[] initialSceneSetup = !EditorApplication.isPlaying ? EditorSceneManager.GetSceneManagerSetup() : null; @@ -142,7 +153,7 @@ namespace AssetUsageDetectorNamespace this.searchDepthLimit = searchParameters.searchDepthLimit; // Initialize commonly used variables - List searchResult = new List(); // Overall search results + searchResult = new List(); // Overall search results if( typeToVariables == null ) typeToVariables = new Dictionary( 4096 ); @@ -229,9 +240,6 @@ namespace AssetUsageDetectorNamespace foreach( Object obj in objectsToSearchSet ) { - // Initialize the nodes of searched asset(s) - searchedObjects.Add( obj.Hash(), PopReferenceNode( obj ) ); - if( obj is Texture ) { searchRenderers = true; @@ -297,6 +305,17 @@ namespace AssetUsageDetectorNamespace // By default, search only serializable variables for references searchSerializableVariablesOnly = true; + // Initialize the nodes of searched asset(s) + foreach( Object obj in objectsToSearchSet ) + { + BeginSearchObject( obj ); + + string objHash = obj.Hash(); + ReferenceNode referenceNode; + if( !searchedObjects.TryGetValue( objHash, out referenceNode ) || referenceNode == null ) + searchedObjects[objHash] = PopReferenceNode( obj ); + } + // Don't search assets if searched object(s) are all scene objects as assets can't hold references to scene objects if( searchParameters.searchInAssetsFolder && assetsToSearchSet.Count > 0 ) { @@ -373,16 +392,27 @@ namespace AssetUsageDetectorNamespace for( int i = 0; i < searchResult.Count; i++ ) searchResult[i].InitializeNodes(); + // If there are any empty groups after node initialization, remove those groups + for( int i = searchResult.Count - 1; i >= 0; i-- ) + { + if( searchResult[i].NumberOfReferences == 0 ) + searchResult.RemoveAt( i ); + } + // Log some c00l stuff to console Debug.Log( "Searched " + searchedObjectsCount + " objects in " + ( EditorApplication.timeSinceStartup - searchStartTime ).ToString( "F2" ) + " seconds" ); - currentSearchResultGroup = null; - return new SearchResult( searchResult, initialSceneSetup ); + return new SearchResult( true, searchResult, initialSceneSetup ); } catch( Exception e ) { Debug.LogException( e ); - return new SearchResult( null, initialSceneSetup ); + return new SearchResult( false, searchResult, initialSceneSetup ); + } + finally + { + currentSearchResultGroup = null; + currentObject = null; } } @@ -503,37 +533,10 @@ namespace AssetUsageDetectorNamespace if( obj is SceneAsset ) return; - if( objectsToSearchSet.Contains( obj ) ) - { - // Rare case: if searched object is a scene GameObject, search its components for references - // instead of completely ignoring the GameObject - if( obj is GameObject && !obj.IsAsset() ) - { - ReferenceNode referenceNode = PopReferenceNode( obj ); - Component[] components = ( (GameObject) obj ).GetComponents(); - for( int i = 0; i < components.Length; i++ ) - { - ReferenceNode componentNode = SearchComponent( components[i] ); - if( componentNode == null ) - continue; - - if( componentNode.NumberOfOutgoingLinks > 0 ) - referenceNode.AddLinkTo( componentNode ); - else - PoolReferenceNode( componentNode ); - } - - if( referenceNode.NumberOfOutgoingLinks > 0 ) - currentSearchResultGroup.AddReference( referenceNode ); - else - PoolReferenceNode( referenceNode ); - } - - return; - } + currentObject = obj; ReferenceNode searchResult = SearchObject( obj ); - if( searchResult != null ) + if( searchResult != null && currentSearchResultGroup != null ) currentSearchResultGroup.AddReference( searchResult ); } @@ -568,20 +571,23 @@ namespace AssetUsageDetectorNamespace // If we hit a searched asset if( objectsToSearchSet.Contains( unityObject ) ) { - result = PopReferenceNode( unityObject ); - searchedObjects.Add( objHash, result ); + // If we were searching for references of the searched asset that we hit (or a component of it), + // ignore the hit since the searched asset doesn't have a meaningful reference to itself at the moment + // If there is a meaningful reference, it will become available when calling the SearchX functions below + if( currentObject != unityObject && ( !( unityObject is Component ) || currentObject != ( (Component) unityObject ).gameObject ) ) + { + result = PopReferenceNode( unityObject ); + searchedObjects.Add( objHash, result ); - return result; + return result; + } } - if( unityObject.IsAsset() ) + // If the Object is an asset, search it in detail only if its dependencies contain at least one of the searched asset(s) + if( unityObject.IsAsset() && !AssetHasAnyReferenceTo( AssetDatabase.GetAssetPath( unityObject ), assetsToSearchPathsSet ) ) { - // If the Object is an asset, search it in detail only if its dependencies contain at least one of the searched asset(s) - if( !AssetHasAnyReferenceTo( AssetDatabase.GetAssetPath( unityObject ), assetsToSearchPathsSet ) ) - { - searchedObjects.Add( objHash, null ); - return null; - } + searchedObjects.Add( objHash, null ); + return null; } callStack.Push( unityObject ); @@ -597,6 +603,10 @@ namespace AssetUsageDetectorNamespace result = SearchAnimatorController( (RuntimeAnimatorController) unityObject ); else if( unityObject is AnimationClip ) result = SearchAnimationClip( (AnimationClip) unityObject ); +#if UNITY_2017_1_OR_NEWER + else if( unityObject is SpriteAtlas ) + result = SearchSpriteAtlas( (SpriteAtlas) unityObject ); +#endif else { result = PopReferenceNode( unityObject ); @@ -636,7 +646,6 @@ namespace AssetUsageDetectorNamespace return result; } - // Search through components of this GameObject in detail private ReferenceNode SearchGameObject( GameObject go ) { ReferenceNode referenceNode = PopReferenceNode( go ); @@ -657,16 +666,11 @@ namespace AssetUsageDetectorNamespace // Search through all the components of the object Component[] components = go.GetComponents(); for( int i = 0; i < components.Length; i++ ) - { - ReferenceNode result = SearchObject( components[i] ); - if( result != null ) - referenceNode.AddLinkTo( result ); - } + referenceNode.AddLinkTo( SearchObject( components[i] ) ); return referenceNode; } - // Check if the asset is used in this component private ReferenceNode SearchComponent( Component component ) { // Ignore Transform component (no object field to search for) @@ -705,12 +709,37 @@ namespace AssetUsageDetectorNamespace referenceNode.AddLinkTo( SearchObject( ( (Animator) component ).runtimeAnimatorController ) ); } +#if UNITY_2017_2_OR_NEWER + if( component is Tilemap ) + { + TileBase[] tiles = new TileBase[( (Tilemap) component ).GetUsedTilesCount()]; + ( (Tilemap) component ).GetUsedTilesNonAlloc( tiles ); + + if( tiles != null ) + { + for( int i = 0; i < tiles.Length; i++ ) + referenceNode.AddLinkTo( SearchObject( tiles[i] ), "Tile" ); + } + } +#endif + +#if UNITY_2017_1_OR_NEWER + if( component is PlayableDirector ) + { + PlayableAsset playableAsset = ( (PlayableDirector) component ).playableAsset; + if( playableAsset != null && !playableAsset.Equals( null ) ) + { + foreach( PlayableBinding binding in playableAsset.outputs ) + referenceNode.AddLinkTo( SearchObject( ( (PlayableDirector) component ).GetGenericBinding( binding.sourceObject ) ), "Binding: " + binding.streamName ); + } + } +#endif + SearchFieldsAndPropertiesOf( referenceNode ); return referenceNode; } - // Check if the asset is used in this material private ReferenceNode SearchMaterial( Material material ) { ReferenceNode referenceNode = PopReferenceNode( material ); @@ -739,7 +768,6 @@ namespace AssetUsageDetectorNamespace return referenceNode; } - // Check if the asset is used in this animator controller private ReferenceNode SearchAnimatorController( RuntimeAnimatorController controller ) { ReferenceNode referenceNode = PopReferenceNode( controller ); @@ -751,7 +779,6 @@ namespace AssetUsageDetectorNamespace return referenceNode; } - // Check if the asset is used in this animation clip (and its keyframes) private ReferenceNode SearchAnimationClip( AnimationClip clip ) { ReferenceNode referenceNode = PopReferenceNode( clip ); @@ -769,6 +796,29 @@ namespace AssetUsageDetectorNamespace return referenceNode; } +#if UNITY_2017_1_OR_NEWER + private ReferenceNode SearchSpriteAtlas( SpriteAtlas spriteAtlas ) + { + ReferenceNode referenceNode = PopReferenceNode( spriteAtlas ); + + if( spriteAtlas.isVariant ) + { + Object masterAtlas = new SerializedObject( spriteAtlas ).FindProperty( "m_MasterAtlas" ).objectReferenceValue; + if( objectsToSearchSet.Contains( masterAtlas ) ) + referenceNode.AddLinkTo( SearchObject( masterAtlas ), "Master Atlas" ); + } + + Object[] packables = spriteAtlas.GetPackables(); + if( packables != null ) + { + for( int i = 0; i < packables.Length; i++ ) + referenceNode.AddLinkTo( SearchObject( packables[i] ), "Packed Texture" ); + } + + return referenceNode; + } +#endif + // Search through field and properties of an object for references private void SearchFieldsAndPropertiesOf( ReferenceNode referenceNode ) { diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs index cee3434..b82f770 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs @@ -291,6 +291,8 @@ namespace AssetUsageDetectorNamespace EditorGUILayout.HelpBox( "ERROR: searchResult is null", MessageType.Error ); return; } + else if( !searchResult.SearchCompletedSuccessfully ) + EditorGUILayout.HelpBox( "ERROR: search was interrupted, check the logs for more info", MessageType.Error ); Color c = GUI.color; GUI.color = Color.green; @@ -299,7 +301,7 @@ namespace AssetUsageDetectorNamespace GUILayout.Space( 10 ); - if( searchResult.Count == 0 ) + if( searchResult.NumberOfGroups == 0 ) GUILayout.Box( "No results found...", Utilities.GL_EXPAND_WIDTH ); else { diff --git a/Plugins/AssetUsageDetector/Editor/SearchResult.cs b/Plugins/AssetUsageDetector/Editor/SearchResult.cs index 0c0d466..26c9f25 100644 --- a/Plugins/AssetUsageDetector/Editor/SearchResult.cs +++ b/Plugins/AssetUsageDetector/Editor/SearchResult.cs @@ -46,23 +46,25 @@ namespace AssetUsageDetectorNamespace public List linkDescriptions; } + private bool success; private List result; private SceneSetup[] initialSceneSetup; private List serializedNodes; private List serializedGroups; - public int Count { get { return result.Count; } } + public int NumberOfGroups { get { return result.Count; } } public SearchResultGroup this[int index] { get { return result[index]; } } - public bool SearchCompletedSuccessfully { get { return result != null; } } + public bool SearchCompletedSuccessfully { get { return success; } } public bool InitialSceneSetupConfigured { get { return initialSceneSetup != null; } } - public SearchResult( List result, SceneSetup[] initialSceneSetup ) + public SearchResult( bool success, List result, SceneSetup[] initialSceneSetup ) { if( result == null ) result = new List( 0 ); + this.success = success; this.result = result; this.initialSceneSetup = initialSceneSetup; } @@ -79,6 +81,7 @@ namespace AssetUsageDetectorNamespace // Show tooltip at mouse position Vector2 mousePos = Event.current.mousePosition; Vector2 size = Utilities.TooltipGUIStyle.CalcSize( new GUIContent( parameters.tooltip ) ); + size.x += 10f; GUI.Box( new Rect( new Vector2( mousePos.x - size.x * 0.5f, mousePos.y - size.y ), size ), parameters.tooltip, Utilities.TooltipGUIStyle ); } @@ -250,8 +253,34 @@ namespace AssetUsageDetectorNamespace // Initializes commonly used variables of the nodes public void InitializeNodes() { - for( int i = 0; i < references.Count; i++ ) - references[i].InitializeRecursively(); + for( int i = references.Count - 1; i >= 0; i-- ) + { + ReferenceNode node = references[i]; + if( node.NumberOfOutgoingLinks == 0 ) + references.RemoveAt( i ); + else + { + // For simplicity's sake, get rid of root nodes that are + // already part of another node's hierarchy + bool isRootNodeChildOfOtherNode = false; + for( int j = references.Count - 1; j >= 0; j-- ) + { + if( i == j ) + continue; + + if( references[j].NodeExistsInChildrenRecursive( node ) ) + { + isRootNodeChildOfOtherNode = true; + break; + } + } + + if( isRootNodeChildOfOtherNode ) + references.RemoveAt( i ); + else + node.InitializeRecursively(); + } + } } // Check if node exists in this results set @@ -476,6 +505,23 @@ namespace AssetUsageDetectorNamespace if( !string.IsNullOrEmpty( description ) ) description = "[" + description + "]"; + // Avoid duplicate links + for( int i = 0; i < links.Count; i++ ) + { + if( links[i].targetNode == nextNode ) + { + if( !string.IsNullOrEmpty( description ) ) + { + if( !string.IsNullOrEmpty( links[i].description ) ) + links[i] = new Link( links[i].targetNode, string.Concat( links[i].description, Environment.NewLine, description ) ); + else + links[i] = new Link( links[i].targetNode, description ); + } + + return; + } + } + links.Add( new Link( nextNode, description ) ); } } @@ -509,6 +555,24 @@ namespace AssetUsageDetectorNamespace links[i].targetNode.InitializeRecursively(); } + // Returns whether or not specified node is part of this node's siblings + public bool NodeExistsInChildrenRecursive( ReferenceNode node ) + { + for( int i = 0; i < links.Count; i++ ) + { + if( links[i].targetNode == node ) + return true; + } + + for( int i = 0; i < links.Count; i++ ) + { + if( links[i].targetNode.NodeExistsInChildrenRecursive( node ) ) + return true; + } + + return false; + } + // Clear this node so that it can be reused later public void Clear() { diff --git a/Plugins/AssetUsageDetector/Editor/Utilities.cs b/Plugins/AssetUsageDetector/Editor/Utilities.cs index 00502af..1a9dd2d 100644 --- a/Plugins/AssetUsageDetector/Editor/Utilities.cs +++ b/Plugins/AssetUsageDetector/Editor/Utilities.cs @@ -130,10 +130,7 @@ namespace AssetUsageDetectorNamespace // If CTRL key is pressed, do a multi-select; // otherwise select only the clicked object and ping it in editor if( !e.control ) - { - obj.PingInEditor(); - Selection.activeObject = obj; - } + Selection.activeObject = obj.PingInEditor(); else { Component objAsComp = obj as Component; @@ -178,11 +175,13 @@ namespace AssetUsageDetectorNamespace } // Ping an object in either Project view or Hierarchy view - public static void PingInEditor( this Object obj ) + public static Object PingInEditor( this Object obj ) { if( obj is Component ) obj = ( (Component) obj ).gameObject; + Object selection = obj; + // Pinging a prefab only works if the pinged object is the root of the prefab // or a direct child of it. Pinging any grandchildren of the prefab // does not work; in which case, traverse the parent hierarchy until @@ -191,6 +190,41 @@ namespace AssetUsageDetectorNamespace { #if UNITY_2018_3_OR_NEWER Transform objTR = ( (GameObject) obj ).transform.root; + + PrefabAssetType prefabAssetType = PrefabUtility.GetPrefabAssetType( objTR.gameObject ); + if( prefabAssetType == PrefabAssetType.Regular || prefabAssetType == PrefabAssetType.Variant ) + { + string assetPath = AssetDatabase.GetAssetPath( objTR.gameObject ); + var openPrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage(); + if( openPrefabStage != null && openPrefabStage.stageHandle.IsValid() && assetPath == openPrefabStage.prefabAssetPath ) + { + // Ping the object in prefab stage + Transform targetParent = objTR; + Transform selectedObject = ( (GameObject) obj ).transform; + objTR = openPrefabStage.prefabContentsRoot.transform; + while( targetParent != selectedObject ) + { + Transform temp = selectedObject; + while( temp.parent != targetParent ) + temp = temp.parent; + + objTR = objTR.GetChild( temp.GetSiblingIndex() ); + targetParent = temp; + } + + selection = objTR.gameObject; + } +#if UNITY_2019_1_OR_NEWER + else if( obj != objTR.gameObject ) + { + Debug.Log( "Open " + assetPath + " in prefab mode to select and edit " + obj.name ); + selection = objTR.gameObject; + } +#else + else + Debug.Log( "Open " + assetPath + " in prefab mode to select and edit " + obj.name ); +#endif + } #else Transform objTR = ( (GameObject) obj ).transform; while( objTR.parent != null && objTR.parent.parent != null ) @@ -201,6 +235,7 @@ namespace AssetUsageDetectorNamespace } EditorGUIUtility.PingObject( obj ); + return selection; } // Check if the field is serializable @@ -286,6 +321,10 @@ namespace AssetUsageDetectorNamespace if( propertyInfo.GetIndexParameters().Length > 0 ) return null; + // Can't use PropertyWrapper (which uses CreateDelegate) for property getters of structs + if( propertyInfo.DeclaringType.IsValueType ) + return propertyInfo.CanRead ? ( ( obj ) => propertyInfo.GetValue( obj, null ) ) : (VariableGetVal) null; + MethodInfo mi = propertyInfo.GetGetMethod( true ); if( mi != null ) {