diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs index 6383592..1a24a9d 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs @@ -184,11 +184,10 @@ namespace AssetUsageDetectorNamespace private readonly Func renderSettingsGetter = (Func) Delegate.CreateDelegate( typeof( Func ), typeof( RenderSettings ).GetMethod( "GetRenderSettings", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) ); private readonly Func defaultReflectionProbeGetter = (Func) Delegate.CreateDelegate( typeof( Func ), typeof( RenderSettings ).GetProperty( "defaultReflection", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ).GetGetMethod( true ) ); - internal static readonly Func spriteAtlasPackedSpritesGetter = (Func) Delegate.CreateDelegate( typeof( Func ), typeof( SpriteAtlasExtensions ).GetMethod( "GetPackedSprites", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) ); #if ASSET_USAGE_ADDRESSABLES - private readonly PropertyInfo assetReferenceSubObjectTypeGetter = - typeof( AssetReference ).GetProperty( "SubObjectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ) - ?? typeof( AssetReference ).GetProperty( "SubOjbectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ); + private readonly Func assetReferenceSubObjectTypeGetter = (Func)Delegate.CreateDelegate(typeof(Func), + typeof(AssetReference).GetProperty("SubObjectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)?.GetMethod + ?? typeof(AssetReference).GetProperty("SubOjbectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).GetMethod); #endif #if ASSET_USAGE_VFX_GRAPH @@ -1810,41 +1809,39 @@ namespace AssetUsageDetectorNamespace } #if ASSET_USAGE_ADDRESSABLES - private Object GetAddressablesAssetReferenceValue( AssetReference assetReference ) - { - Object result = assetReference.editorAsset; - if( !result ) - return null; + private Object GetAddressablesAssetReferenceValue(AssetReference assetReference) + { + Object result = assetReference.editorAsset; + if (result == null) + 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]; - } - } - } + string subObjectName = assetReference.SubObjectName; + if (string.IsNullOrEmpty(subObjectName)) + return result; - return result; - } + Type subObjectType = assetReferenceSubObjectTypeGetter(assetReference) ?? typeof(Object); + if (subObjectType.IsAssignableFrom(result.GetType())) + return result; + + if (result is SpriteAtlas spriteAtlas) + { + foreach (Sprite sprite in spriteAtlas.GetPackedSprites()) + { + if (sprite != null && sprite.name == subObjectName) + return sprite; + } + } + else + { + foreach (Object subAsset in AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(result))) + { + if (subAsset != null && subAsset.name == subObjectName && subObjectType.IsAssignableFrom(subAsset.GetType())) + return subAsset; + } + } + + return result; + } #endif private bool ShouldExcludeRedundantPrefabReferences( Object obj ) @@ -1889,4 +1886,4 @@ namespace AssetUsageDetectorNamespace return ( component != null && !component.Equals( null ) ) ? component.gameObject : obj; } } -} \ No newline at end of file +} diff --git a/Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs b/Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs index 0d57a1b..f063fdf 100644 --- a/Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs +++ b/Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs @@ -99,15 +99,11 @@ namespace AssetUsageDetectorNamespace // Add Sprites of SpriteAtlases to the sub-assets list if( target is SpriteAtlas spriteAtlas ) { - Sprite[] packedSprites = AssetUsageDetector.spriteAtlasPackedSpritesGetter( spriteAtlas ); - if( packedSprites != null ) - { - for( int i = 0; i < packedSprites.Length; i++ ) - { - if( packedSprites[i] != null && currentSubAssets.Add( packedSprites[i] ) ) - subAssets.Add( new SubAsset( packedSprites[i], shouldSearchChildren ?? true ) ); - } - } + foreach (Sprite sprite in spriteAtlas.GetPackedSprites()) + { + if (sprite != null && currentSubAssets.Add(sprite)) + subAssets.Add(new SubAsset(sprite, shouldSearchChildren ?? true)); + } } // Find sub-asset(s) of the asset (if any) diff --git a/Plugins/AssetUsageDetector/Editor/Utilities.cs b/Plugins/AssetUsageDetector/Editor/Utilities.cs index 8261df4..4102d1e 100644 --- a/Plugins/AssetUsageDetector/Editor/Utilities.cs +++ b/Plugins/AssetUsageDetector/Editor/Utilities.cs @@ -6,8 +6,10 @@ using System.Threading.Tasks; using Unity.Collections; using UnityEditor; using UnityEditor.SceneManagement; +using UnityEditor.U2D; using UnityEngine; using UnityEngine.SceneManagement; +using UnityEngine.U2D; using Object = UnityEngine.Object; namespace AssetUsageDetectorNamespace @@ -564,5 +566,42 @@ namespace AssetUsageDetectorNamespace return false; } + + public static List GetPackedSprites(this SpriteAtlas spriteAtlas) + { + SpriteAtlas masterAtlas = spriteAtlas.isVariant ? spriteAtlas.GetMasterAtlas() : null; + Object[] packables = (masterAtlas != null) ? masterAtlas.GetPackables() : spriteAtlas.GetPackables(); + List result = new(packables.Length); + + foreach (Object packable in packables) + { + if (packable == null) + continue; + + if (packable.IsFolder()) + { + foreach (string guid in AssetDatabase.FindAssets("t:Sprite", new[] { AssetDatabase.GetAssetPath(packable) })) + { + foreach (Object asset in AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GUIDToAssetPath(guid))) + { + if (asset is Sprite sprite) + result.Add(sprite); + } + } + } + else if (packable is Texture2D) + { + foreach (Object asset in AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(packable))) + { + if (asset is Sprite sprite) + result.Add(sprite); + } + } + else if (packable is Sprite sprite) + result.Add(sprite); + } + + return result; + } } -} \ No newline at end of file +}