diff --git a/.github/README.md b/.github/README.md index 37246a2..9601221 100644 --- a/.github/README.md +++ b/.github/README.md @@ -28,6 +28,7 @@ There are 5 ways to install this plugin: - Open **Window - Asset Usage Detector** window, configure the settings and hit **GO!** - or, right click an object and select **Search For References** +- In the search results page, each row represents a reference to the searched Object(s). Rows can be traversed from left to right to see how the references are formed. If a node's label contains text inside square brackets (e.g. *\[Child object\]*), then that text describes how that node is connected to the node to its left ## FEATURES diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs index 11b7e67..5778e91 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs @@ -14,10 +14,13 @@ using UnityEngine.UI; using System.Text; #if UNITY_2017_1_OR_NEWER using UnityEngine.U2D; +using UnityEngine.Playables; +#endif #if UNITY_2018_2_OR_NEWER using UnityEditor.U2D; #endif -using UnityEngine.Playables; +#if UNITY_2017_3_OR_NEWER +using UnityEditor.Compilation; #endif #if UNITY_2017_2_OR_NEWER using UnityEngine.Tilemaps; @@ -104,6 +107,17 @@ namespace AssetUsageDetectorNamespace } } } + +#if UNITY_2017_3_OR_NEWER +#pragma warning disable 0649 // The fields' values are assigned via JsonUtility + [Serializable] + private struct AssemblyDefinitionReferences + { + public string reference; // Used by AssemblyDefinitionReferenceAssets + public List references; // Used by AssemblyDefinitionAssets + } +#pragma warning restore 0649 +#endif #endregion // A set that contains the searched asset(s) and their sub-assets (if any) @@ -123,6 +137,11 @@ namespace AssetUsageDetectorNamespace // Path(s) of the assets that should be excluded from the search private readonly HashSet excludedAssetsPathsSet = new HashSet(); +#if UNITY_2017_3_OR_NEWER + // Path(s) of the Assembly Definition Files in objectsToSearchSet (Value: files themselves) + private readonly Dictionary assemblyDefinitionFilesToSearch = new Dictionary( 8 ); +#endif + // Results for the currently searched scene private SearchResultGroup currentSearchResultGroup; @@ -258,6 +277,9 @@ namespace AssetUsageDetectorNamespace assetsToSearchPathsSet.Clear(); assetsToSearchRootPrefabs.Clear(); excludedAssetsPathsSet.Clear(); +#if UNITY_2017_3_OR_NEWER + assemblyDefinitionFilesToSearch.Clear(); +#endif if( assetDependencyCache == null ) { @@ -291,6 +313,12 @@ namespace AssetUsageDetectorNamespace { typeof( AnimationClip ), SearchAnimationClip }, #if UNITY_2017_1_OR_NEWER { typeof( SpriteAtlas ), SearchSpriteAtlas }, +#endif +#if UNITY_2017_3_OR_NEWER + { typeof( UnityEditorInternal.AssemblyDefinitionAsset ), SearchAssemblyDefinitionFile }, +#endif +#if UNITY_2019_2_OR_NEWER + { typeof( UnityEditorInternal.AssemblyDefinitionReferenceAsset ), SearchAssemblyDefinitionFile }, #endif }; } @@ -360,7 +388,7 @@ namespace AssetUsageDetectorNamespace // Find the scenes to search for references HashSet scenesToSearch = new HashSet(); - if( searchParameters.searchInScenesSubset != null ) + if( searchParameters.searchInScenesSubset != null && searchParameters.searchInScenesSubset.Length > 0 ) { foreach( Object obj in searchParameters.searchInScenesSubset ) { @@ -456,7 +484,7 @@ namespace AssetUsageDetectorNamespace // Get the paths of all assets that are to be searched IEnumerable assetPaths; - if( searchParameters.searchInAssetsSubset == null ) + if( searchParameters.searchInAssetsSubset == null || searchParameters.searchInAssetsSubset.Length == 0 ) { string[] allAssetPaths = AssetDatabase.GetAllAssetPaths(); assetPaths = allAssetPaths; @@ -506,6 +534,25 @@ namespace AssetUsageDetectorNamespace } } +#if UNITY_2017_3_OR_NEWER + if( assemblyDefinitionFilesToSearch.Count > 0 ) + { + // AssetDatabase.GetDependencies doesn't return references from Assembly Definition Files to their Assembly Definition References, + // so if we are searching for an Assembly Definition File's usages, we must search all Assembly Definition Files' references + // manually. By adding all these files to assetsToSearchPathsSet, we make sure that AssetHasAnyReference returns true for + // these assets and they don't get excluded from the search + string[] assemblyDefinitionFilesGuids = AssetDatabase.FindAssets( "t:AssemblyDefinitionAsset" ); + for( int i = 0; i < assemblyDefinitionFilesGuids.Length; i++ ) + assetsToSearchPathsSet.Add( AssetDatabase.GUIDToAssetPath( assemblyDefinitionFilesGuids[i] ) ); + +#if UNITY_2019_2_OR_NEWER + assemblyDefinitionFilesGuids = AssetDatabase.FindAssets( "t:AssemblyDefinitionReferenceAsset" ); + for( int i = 0; i < assemblyDefinitionFilesGuids.Length; i++ ) + assetsToSearchPathsSet.Add( AssetDatabase.GUIDToAssetPath( assemblyDefinitionFilesGuids[i] ) ); +#endif + } +#endif + if( EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Searching assets", 0f ) ) throw new Exception( "Search aborted" ); @@ -760,6 +807,13 @@ namespace AssetUsageDetectorNamespace if( shouldAddRootPrefabEntry ) assetsToSearchRootPrefabs.Add( go ); } +#if UNITY_2017_3_OR_NEWER + else if( obj is UnityEditorInternal.AssemblyDefinitionAsset ) + { + if( !string.IsNullOrEmpty( assetPath ) ) + assemblyDefinitionFilesToSearch[assetPath] = obj; + } +#endif } else { @@ -931,7 +985,7 @@ namespace AssetUsageDetectorNamespace else { result = PopReferenceNode( unityObject ); - SearchWithSerializedObject( result ); + SearchVariablesWithSerializedObject( result ); } callStack.RemoveAt( callStack.Count - 1 ); @@ -946,7 +1000,7 @@ namespace AssetUsageDetectorNamespace currentDepth++; result = PopReferenceNode( obj ); - SearchFieldsAndPropertiesOf( result ); + SearchVariablesWithReflection( result ); currentDepth--; callStack.RemoveAt( callStack.Count - 1 ); @@ -1069,7 +1123,7 @@ namespace AssetUsageDetectorNamespace } #endif - SearchWithSerializedObject( referenceNode ); + SearchVariablesWithSerializedObject( referenceNode ); return referenceNode; } @@ -1251,6 +1305,43 @@ namespace AssetUsageDetectorNamespace } #endif +#if UNITY_2017_3_OR_NEWER + // Find references from an Assembly Definition File to its Assembly Definition References + private ReferenceNode SearchAssemblyDefinitionFile( Object unityObject ) + { + if( assemblyDefinitionFilesToSearch.Count == 0 ) + return null; + + AssemblyDefinitionReferences assemblyDefinitionFile = JsonUtility.FromJson( ( (TextAsset) unityObject ).text ); + ReferenceNode referenceNode = PopReferenceNode( unityObject ); + + if( !string.IsNullOrEmpty( assemblyDefinitionFile.reference ) ) + { + if( assemblyDefinitionFile.references == null ) + assemblyDefinitionFile.references = new List( 1 ) { assemblyDefinitionFile.reference }; + else + assemblyDefinitionFile.references.Add( assemblyDefinitionFile.reference ); + } + + if( assemblyDefinitionFile.references != null ) + { + for( int i = 0; i < assemblyDefinitionFile.references.Count; i++ ) + { +#if UNITY_2019_1_OR_NEWER + string assemblyPath = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyReference( assemblyDefinitionFile.references[i] ); +#else + string assemblyPath = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName( assemblyDefinitionFile.references[i] ); +#endif + Object searchedAssemblyDefinitionFile; + if( assemblyDefinitionFilesToSearch.TryGetValue( assemblyPath, out searchedAssemblyDefinitionFile ) ) + referenceNode.AddLinkTo( GetReferenceNode( searchedAssemblyDefinitionFile ), "Referenced Assembly" ); + } + } + + return referenceNode; + } +#endif + // Find references from an Animation/Animator component to the objects that it animates private void SearchAnimatedObjects( ReferenceNode referenceNode ) { @@ -1330,8 +1421,8 @@ namespace AssetUsageDetectorNamespace } } - // Search through field and properties of an object for references with SerializedObject - private void SearchWithSerializedObject( ReferenceNode referenceNode ) + // Search through variables of an object with SerializedObject + private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode ) { if( !isInPlayMode || referenceNode.nodeObject.IsAsset() ) { @@ -1378,11 +1469,11 @@ namespace AssetUsageDetectorNamespace } // Use reflection algorithm as fallback - SearchFieldsAndPropertiesOf( referenceNode ); + SearchVariablesWithReflection( referenceNode ); } - // Search through field and properties of an object for references - private void SearchFieldsAndPropertiesOf( ReferenceNode referenceNode ) + // Search through variables of an object with reflection + private void SearchVariablesWithReflection( ReferenceNode referenceNode ) { // Get filtered variables for this object VariableGetterHolder[] variables = GetFilteredVariablesForType( referenceNode.nodeObject.GetType() ); diff --git a/Plugins/AssetUsageDetector/Editor/SearchResult.cs b/Plugins/AssetUsageDetector/Editor/SearchResult.cs index ef52290..695b51c 100644 --- a/Plugins/AssetUsageDetector/Editor/SearchResult.cs +++ b/Plugins/AssetUsageDetector/Editor/SearchResult.cs @@ -217,7 +217,7 @@ namespace AssetUsageDetectorNamespace // To work nicely with GUILayout and scroll view EditorGUILayout.GetControlRect( Utilities.GL_EXPAND_WIDTH, GUILayout.Height( guiHeight ) ); - if( parameters.tooltip != null ) + if( !string.IsNullOrEmpty( parameters.tooltip ) ) { // Show tooltip at mouse position Vector2 mousePos = Event.current.mousePosition; diff --git a/Plugins/AssetUsageDetector/Editor/Utilities.cs b/Plugins/AssetUsageDetector/Editor/Utilities.cs index b499ed8..5d05612 100644 --- a/Plugins/AssetUsageDetector/Editor/Utilities.cs +++ b/Plugins/AssetUsageDetector/Editor/Utilities.cs @@ -99,6 +99,7 @@ namespace AssetUsageDetectorNamespace normalState = m_tooltipGUIStyle.normal; normalState.background = null; + normalState.scaledBackgrounds = new Texture2D[0]; normalState.textColor = Color.black; } diff --git a/Plugins/AssetUsageDetector/README.txt b/Plugins/AssetUsageDetector/README.txt index 27f5a03..35dd350 100644 --- a/Plugins/AssetUsageDetector/README.txt +++ b/Plugins/AssetUsageDetector/README.txt @@ -9,6 +9,10 @@ This tool helps you find usages of the selected asset(s) and/or Object(s) in you 2. HOW TO Open "Window-Asset Usage Detector" window, configure the settings and hit GO! You can also right click an object and select "Search For References". +In the search results page, each row represents a reference to the searched Object(s). Rows can be traversed from left to right to see how the +references are formed. If a node's label contains text inside square brackets (e.g. [Child object]), then that text describes how that node is +connected to the node to its left. + 3. KNOWN LIMITATIONS - static variables are not searched - GUIText materials are not searched