Skip to content

Commit 5104901

Browse files
authored
Searching for usages of SpriteAtlas will now include its Sprites, as well (#43)
1 parent 962f6e0 commit 5104901

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ private static string ExtractGUIDFromString( string str )
211211
private readonly Func<Cubemap> defaultReflectionProbeGetter = (Func<Cubemap>) Delegate.CreateDelegate( typeof( Func<Cubemap> ), typeof( RenderSettings ).GetProperty( "defaultReflection", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ).GetGetMethod( true ) );
212212
#endif
213213

214+
internal static readonly Func<SpriteAtlas, Sprite[]> spriteAtlasPackedSpritesGetter = (Func<SpriteAtlas, Sprite[]>) Delegate.CreateDelegate( typeof( Func<SpriteAtlas, Sprite[]> ), typeof( SpriteAtlasExtensions ).GetMethod( "GetPackedSprites", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) );
214215
#if ASSET_USAGE_ADDRESSABLES
215-
private readonly Func<SpriteAtlas, Sprite[]> spriteAtlasPackedSpritesGetter = (Func<SpriteAtlas, Sprite[]>) Delegate.CreateDelegate( typeof( Func<SpriteAtlas, Sprite[]> ), typeof( SpriteAtlasExtensions ).GetMethod( "GetPackedSprites", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) );
216216
private readonly PropertyInfo assetReferenceSubObjectTypeGetter = typeof( AssetReference ).GetProperty( "SubOjbectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
217217
#endif
218218

Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using UnityEditor;
44
using UnityEngine;
5+
using UnityEngine.U2D;
56
using Object = UnityEngine.Object;
67

78
namespace AssetUsageDetectorNamespace
@@ -95,6 +96,20 @@ private void AddSubAssets( Object target, bool includeTarget, bool? shouldSearch
9596
}
9697
}
9798

99+
// If is an Atlas we get sprites directly as GetSubAssets return the final texture
100+
if( target is SpriteAtlas spriteAtlas )
101+
{
102+
Sprite[] packedSprites = AssetUsageDetector.spriteAtlasPackedSpritesGetter( spriteAtlas );
103+
if( packedSprites != null )
104+
{
105+
for( int i = 0; i < packedSprites.Length; i++ )
106+
{
107+
if( packedSprites[i] != null && currentSubAssets.Add( packedSprites[i] ) )
108+
subAssets.Add( new SubAsset( packedSprites[i], shouldSearchChildren ?? true ) );
109+
}
110+
}
111+
}
112+
98113
// Find sub-asset(s) of the asset (if any)
99114
Object[] assets = AssetDatabase.LoadAllAssetsAtPath( AssetDatabase.GetAssetPath( target ) );
100115
for( int i = 0; i < assets.Length; i++ )
@@ -117,4 +132,4 @@ private void AddSubAssets( Object target, bool includeTarget, bool? shouldSearch
117132
}
118133
}
119134
}
120-
}
135+
}

0 commit comments

Comments
 (0)