diff --git a/.github/Images/AddressablesAssemblyChanges.png b/.github/Images/AddressablesAssemblyChanges.png new file mode 100644 index 0000000..e14f82c Binary files /dev/null and b/.github/Images/AddressablesAssemblyChanges.png differ diff --git a/.github/README.md b/.github/README.md index e2f4e1b..69e53f8 100644 --- a/.github/README.md +++ b/.github/README.md @@ -37,6 +37,18 @@ There are 5 ways to install this plugin: ![SearchResults2](Images/SearchResults2Dark.png) +## ADDRESSABLES SUPPORT + +This plugin has experimental support for *Addressables* package. However, the search may take significantly longer to finish when Addressables are searched. Some manual modifications are needed to enable Addressables support: + +- The plugin mustn't be installed as a package, i.e. it must reside inside the *Assets* folder and not the *Packages* folder (it can reside inside a subfolder of Assets like *Assets/Plugins*) +- Modify the **AssetUsageDetector.Editor** Assembly Definition File as follows: + +![AddressablesAssemblyChanges](Images/AddressablesAssemblyChanges.png) + +- If *Version Defines* isn't present (on older Unity versions), manually add `ASSET_USAGE_ADDRESSABLES` compiler directive to **Player Settings/Scripting Define Symbols** (these symbols are platform specific, so if you change the active platform later, you'll have to add the compiler directive again) +- Enable the "Addressables support" option in Asset Usage Detector window + ## SEARCH REFACTORING While searching for references using the Scripting API, it's possible to get notified of the found references *during* the search (some references like *Assembly Definition File* references or *Shader Graph* references aren't supported) and in most cases, refactor them (e.g. changing all usages of a searched object with something else or *null*). Disabling *Lazy Scene Search* is recommended while using this feature and it's advised to backup your project beforehand. diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs index 417ac6e..5b2dab8 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs @@ -47,6 +47,9 @@ namespace AssetUsageDetectorNamespace public SearchRefactoring searchRefactoring = null; public bool lazySceneSearch = true; +#if ASSET_USAGE_ADDRESSABLES + public bool addressablesSupport = false; +#endif public bool calculateUnusedObjects = false; public bool hideDuplicateRows = true; public bool hideReduntantPrefabVariantLinks = true; @@ -1103,6 +1106,11 @@ namespace AssetUsageDetectorNamespace // Check if the asset at specified path depends on any of the references private bool AssetHasAnyReference( string assetPath ) { +#if ASSET_USAGE_ADDRESSABLES + if( searchParameters.addressablesSupport ) + return true; +#endif + if( assetsToSearchPathsSet.Contains( assetPath ) ) return true; diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs index 9cd8dd2..b7cddb2 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs @@ -21,6 +21,9 @@ using UnityEditor.Compilation; #if UNITY_2017_2_OR_NEWER using UnityEngine.Tilemaps; #endif +#if ASSET_USAGE_ADDRESSABLES +using UnityEngine.AddressableAssets; +#endif using Object = UnityEngine.Object; namespace AssetUsageDetectorNamespace @@ -198,6 +201,12 @@ namespace AssetUsageDetectorNamespace private delegate FieldInfo FieldInfoGetter( SerializedProperty p, out Type t ); private FieldInfoGetter fieldInfoGetter; +#if ASSET_USAGE_ADDRESSABLES + private delegate Sprite[] SpriteAtlasPackedSpritesGetter( SpriteAtlas atlas ); + private SpriteAtlasPackedSpritesGetter spriteAtlasPackedSpritesGetter; + private PropertyInfo assetReferenceSubObjectTypeGetter; +#endif + private void InitializeSearchFunctionsData( Parameters searchParameters ) { if( typeToSearchFunction == null ) @@ -346,6 +355,12 @@ namespace AssetUsageDetectorNamespace MethodInfo fieldInfoGetterMethod = typeof( Editor ).Assembly.GetType( "UnityEditor.ScriptAttributeUtility" ).GetMethod( "GetFieldInfoFromProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ); #endif fieldInfoGetter = (FieldInfoGetter) Delegate.CreateDelegate( typeof( FieldInfoGetter ), fieldInfoGetterMethod ); + +#if ASSET_USAGE_ADDRESSABLES + MethodInfo spriteAtlasPackedSpritesGetterMethod = typeof( SpriteAtlasExtensions ).GetMethod( "GetPackedSprites", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ); + spriteAtlasPackedSpritesGetter = (SpriteAtlasPackedSpritesGetter) Delegate.CreateDelegate( typeof( SpriteAtlasPackedSpritesGetter ), spriteAtlasPackedSpritesGetterMethod ); + assetReferenceSubObjectTypeGetter = typeof( AssetReference ).GetProperty( "SubOjbectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ); +#endif } private ReferenceNode SearchGameObject( Object unityObject ) @@ -1355,6 +1370,12 @@ namespace AssetUsageDetectorNamespace { if( !isInPlayMode || referenceNode.nodeObject.IsAsset() ) { +#if ASSET_USAGE_ADDRESSABLES + // See: https://github.com/yasirkula/UnityAssetUsageDetector/issues/29 + if( searchParameters.addressablesSupport && ( (Object) referenceNode.nodeObject ).name == "Deprecated EditorExtensionImpl" ) + return; +#endif + SerializedObject so = new SerializedObject( (Object) referenceNode.nodeObject ); SerializedProperty iterator = so.GetIterator(); SerializedProperty iteratorVisible = so.GetIterator(); @@ -1404,9 +1425,22 @@ namespace AssetUsageDetectorNamespace break; #endif case SerializedPropertyType.Generic: - propertyValue = null; - searchResult = null; - enterChildren = true; +#if ASSET_USAGE_ADDRESSABLES + AssetReference assetReference; + if( searchParameters.addressablesSupport && iterator.type.StartsWithFast( "AssetReference" ) && ( assetReference = GetRawSerializedPropertyValue( iterator ) as AssetReference ) != null ) + { + propertyValue = GetAddressablesAssetReferenceValue( assetReference ); + searchResult = SearchObject( PreferablyGameObject( propertyValue ) ); + enterChildren = false; + } + else +#endif + { + propertyValue = null; + searchResult = null; + enterChildren = true; + } + break; default: propertyValue = null; @@ -1459,6 +1493,15 @@ namespace AssetUsageDetectorNamespace // no need to have duplicate search entries if( !( variableValue is ICollection ) ) { +#if ASSET_USAGE_ADDRESSABLES + if( searchParameters.addressablesSupport && variableValue is AssetReference ) + { + variableValue = GetAddressablesAssetReferenceValue( (AssetReference) variableValue ); + if( variableValue == null || variableValue.Equals( null ) ) + continue; + } +#endif + ReferenceNode searchResult = SearchObject( PreferablyGameObject( variableValue ) ); if( searchResult != null && searchResult != referenceNode ) { @@ -1741,6 +1784,44 @@ namespace AssetUsageDetectorNamespace return enumerator.Current; } +#if ASSET_USAGE_ADDRESSABLES + private Object GetAddressablesAssetReferenceValue( AssetReference assetReference ) + { + Object result = assetReference.editorAsset; + if( !result ) + return null; + + string subObjectName = assetReference.SubObjectName; + if( !string.IsNullOrEmpty( subObjectName ) ) + { + if( result is SpriteAtlas ) + { + Sprite[] packedSprites = spriteAtlasPackedSpritesGetter( (SpriteAtlas) result ); + if( packedSprites != null ) + { + for( int i = 0; i < packedSprites.Length; i++ ) + { + if( packedSprites[i] && packedSprites[i].name == subObjectName ) + return packedSprites[i]; + } + } + } + else + { + Type subObjectType = (Type) assetReferenceSubObjectTypeGetter.GetValue( assetReference, null ) ?? typeof( Object ); + Object[] subAssets = AssetDatabase.LoadAllAssetRepresentationsAtPath( AssetDatabase.GetAssetPath( result ) ); + for( int k = 0; k < subAssets.Length; k++ ) + { + if( subAssets[k] && subAssets[k].name == subObjectName && subObjectType.IsAssignableFrom( subAssets[k].GetType() ) ) + return subAssets[k]; + } + } + } + + return result; + } +#endif + // Iterates over all occurrences of specific key-value pairs in string // Example1: #include "VALUE" valuePrefix=#include, valueWrapperChar=" // Example2: "guid": "VALUE" valuePrefix="guid", valueWrapperChar=" diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs index c632717..5e46131 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs @@ -31,6 +31,7 @@ namespace AssetUsageDetectorNamespace private const string PREFS_SEARCH_NON_SERIALIZABLES = "AUD_NonSerializables"; private const string PREFS_SEARCH_UNUSED_MATERIAL_PROPERTIES = "AUD_SearchUnusedMaterialProps"; private const string PREFS_LAZY_SCENE_SEARCH = "AUD_LazySceneSearch"; + private const string PREFS_ADDRESSABLES_SUPPORT = "AUD_AddressablesSupport"; private const string PREFS_CALCULATE_UNUSED_OBJECTS = "AUD_FindUnusedObjs"; private const string PREFS_HIDE_DUPLICATE_ROWS = "AUD_HideDuplicates"; private const string PREFS_HIDE_REDUNDANT_PREFAB_VARIANT_LINKS = "AUD_HideRedundantPVariantLinks"; @@ -83,6 +84,9 @@ namespace AssetUsageDetectorNamespace private int searchDepthLimit = 4; // Depth limit for recursively searching variables of objects private bool lazySceneSearch = true; +#if ASSET_USAGE_ADDRESSABLES + private bool addressablesSupport = false; +#endif private bool searchNonSerializableVariables = true; private bool searchUnusedMaterialProperties = true; private bool calculateUnusedObjects = false; @@ -319,6 +323,9 @@ namespace AssetUsageDetectorNamespace searchUnusedMaterialProperties = searchParameters.searchUnusedMaterialProperties; searchRefactoring = searchParameters.searchRefactoring; lazySceneSearch = searchParameters.lazySceneSearch; +#if ASSET_USAGE_ADDRESSABLES + addressablesSupport = searchParameters.addressablesSupport; +#endif calculateUnusedObjects = searchParameters.calculateUnusedObjects; hideDuplicateRows = searchParameters.hideDuplicateRows; hideReduntantPrefabVariantLinks = searchParameters.hideReduntantPrefabVariantLinks; @@ -398,6 +405,9 @@ namespace AssetUsageDetectorNamespace EditorPrefs.SetBool( PREFS_SEARCH_NON_SERIALIZABLES, searchNonSerializableVariables ); EditorPrefs.SetBool( PREFS_SEARCH_UNUSED_MATERIAL_PROPERTIES, searchUnusedMaterialProperties ); EditorPrefs.SetBool( PREFS_LAZY_SCENE_SEARCH, lazySceneSearch ); +#if ASSET_USAGE_ADDRESSABLES + EditorPrefs.SetBool( PREFS_ADDRESSABLES_SUPPORT, addressablesSupport ); +#endif EditorPrefs.SetBool( PREFS_CALCULATE_UNUSED_OBJECTS, calculateUnusedObjects ); EditorPrefs.SetBool( PREFS_HIDE_DUPLICATE_ROWS, hideDuplicateRows ); EditorPrefs.SetBool( PREFS_HIDE_REDUNDANT_PREFAB_VARIANT_LINKS, hideReduntantPrefabVariantLinks ); @@ -417,6 +427,9 @@ namespace AssetUsageDetectorNamespace searchNonSerializableVariables = EditorPrefs.GetBool( PREFS_SEARCH_NON_SERIALIZABLES, true ); searchUnusedMaterialProperties = EditorPrefs.GetBool( PREFS_SEARCH_UNUSED_MATERIAL_PROPERTIES, true ); lazySceneSearch = EditorPrefs.GetBool( PREFS_LAZY_SCENE_SEARCH, true ); +#if ASSET_USAGE_ADDRESSABLES + addressablesSupport = EditorPrefs.GetBool( PREFS_ADDRESSABLES_SUPPORT, false ); +#endif calculateUnusedObjects = EditorPrefs.GetBool( PREFS_CALCULATE_UNUSED_OBJECTS, false ); hideDuplicateRows = EditorPrefs.GetBool( PREFS_HIDE_DUPLICATE_ROWS, true ); hideReduntantPrefabVariantLinks = EditorPrefs.GetBool( PREFS_HIDE_REDUNDANT_PREFAB_VARIANT_LINKS, true ); @@ -551,7 +564,14 @@ namespace AssetUsageDetectorNamespace GUILayout.Box( "SETTINGS", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH ); GUI.backgroundColor = c; +#if ASSET_USAGE_ADDRESSABLES + EditorGUI.BeginDisabledGroup( addressablesSupport ); +#endif lazySceneSearch = WordWrappingToggleLeft( "Lazy scene search: scenes are searched in detail only when they are manually refreshed (faster search)", lazySceneSearch ); +#if ASSET_USAGE_ADDRESSABLES + EditorGUI.EndDisabledGroup(); + addressablesSupport = WordWrappingToggleLeft( "Addressables support (WARNING: 'Lazy scene search' will be disabled) (slower search)", addressablesSupport ); +#endif calculateUnusedObjects = WordWrappingToggleLeft( "Calculate unused objects", calculateUnusedObjects ); hideDuplicateRows = WordWrappingToggleLeft( "Hide duplicate rows in search results", hideDuplicateRows ); #if UNITY_2018_3_OR_NEWER @@ -687,7 +707,12 @@ namespace AssetUsageDetectorNamespace //searchNonSerializableVariables = searchNonSerializableVariables, searchUnusedMaterialProperties = searchUnusedMaterialProperties, searchRefactoring = searchRefactoring, +#if ASSET_USAGE_ADDRESSABLES + lazySceneSearch = lazySceneSearch && !addressablesSupport, + addressablesSupport = addressablesSupport, +#else lazySceneSearch = lazySceneSearch, +#endif calculateUnusedObjects = calculateUnusedObjects, hideDuplicateRows = hideDuplicateRows, hideReduntantPrefabVariantLinks = hideReduntantPrefabVariantLinks, diff --git a/Plugins/AssetUsageDetector/README.txt b/Plugins/AssetUsageDetector/README.txt index bcbbbfe..55daf8b 100644 --- a/Plugins/AssetUsageDetector/README.txt +++ b/Plugins/AssetUsageDetector/README.txt @@ -1,4 +1,4 @@ -= Asset Usage Detector (v2.2.3) = += Asset Usage Detector (v2.3.0) = Online documentation available at: https://github.com/yasirkula/UnityAssetUsageDetector E-mail: yasirkula@gmail.com @@ -8,6 +8,15 @@ E-mail: yasirkula@gmail.com This tool helps you find usages of the selected asset(s) and/or scene object(s), i.e. lists the objects that refer to them. +### ADDRESSABLES SUPPORT +This plugin has experimental support for Addressables package. However, the search may take significantly longer to finish when Addressables are searched. Some manual modifications are needed to enable Addressables support: + +- The plugin mustn't be installed as a package, i.e. it must reside inside the Assets folder and not the Packages folder (it can reside inside a subfolder of Assets like Assets/Plugins) +- Add ASSET_USAGE_ADDRESSABLES compiler directive to "Player Settings/Scripting Define Symbols" (these symbols are platform specific, so if you change the active platform later, you'll have to add the compiler directive again) +- Add "Unity.Addressables" assembly to "AssetUsageDetector.Editor" Assembly Definition File's "Assembly Definition References" list +- Enable the "Addressables support" option in Asset Usage Detector window + + ### 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" diff --git a/package.json b/package.json index 9187ce3..8e8cc39 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.yasirkula.assetusagedetector", "displayName": "Asset Usage Detector", - "version": "2.2.3", + "version": "2.3.0", "documentationUrl": "https://github.com/yasirkula/UnityAssetUsageDetector", "changelogUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/releases", "licensesUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/blob/master/LICENSE.txt",