From 5104901c5d1c9c1f830893c13ef35efa7756756b Mon Sep 17 00:00:00 2001 From: arkms Date: Mon, 19 May 2025 13:08:46 -0400 Subject: [PATCH] Searching for usages of SpriteAtlas will now include its Sprites, as well (#43) --- .../Editor/AssetUsageDetectorSearchFunctions.cs | 2 +- .../AssetUsageDetector/Editor/ObjectToSearch.cs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs index d69e526..5c97055 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs @@ -211,8 +211,8 @@ namespace AssetUsageDetectorNamespace private readonly Func defaultReflectionProbeGetter = (Func) Delegate.CreateDelegate( typeof( Func ), typeof( RenderSettings ).GetProperty( "defaultReflection", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ).GetGetMethod( true ) ); #endif + 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 Func spriteAtlasPackedSpritesGetter = (Func) Delegate.CreateDelegate( typeof( Func ), typeof( SpriteAtlasExtensions ).GetMethod( "GetPackedSprites", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) ); private readonly PropertyInfo assetReferenceSubObjectTypeGetter = typeof( AssetReference ).GetProperty( "SubOjbectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ); #endif diff --git a/Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs b/Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs index 24ad8ac..855c984 100644 --- a/Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs +++ b/Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using UnityEditor; using UnityEngine; +using UnityEngine.U2D; using Object = UnityEngine.Object; namespace AssetUsageDetectorNamespace @@ -95,6 +96,20 @@ namespace AssetUsageDetectorNamespace } } + // If is an Atlas we get sprites directly as GetSubAssets return the final texture + 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 ) ); + } + } + } + // Find sub-asset(s) of the asset (if any) Object[] assets = AssetDatabase.LoadAllAssetsAtPath( AssetDatabase.GetAssetPath( target ) ); for( int i = 0; i < assets.Length; i++ ) @@ -117,4 +132,4 @@ namespace AssetUsageDetectorNamespace } } } -} \ No newline at end of file +}