In my project, I have a lot of individual Sprites that are packed in different SpriteAtlas assets. I’m making a tool, trying to make sure that there’s not Sprite left behind and they all are included in one (and only one) SpriteAtlas.
However, this tool must work in Editor mode, and all Sprites property “packed” is false, as if no sprites are actually packed anywhere. I tried to use the .GetPackables() extension method, but it just deturns DefaultAsset for included folders, and I can’t even get the folder path from that class.
So, I don’t see any way to detect if any given sprite is actually included in any SpriteAtlas from script. Am I missing something?
@golergka You can use AssetDatabase.GetAssetPath to get the path of DefaultAsset. All sprites within that folder should be in the Atlas. Please find sample code below :
// Assuming we have a valid atlas and the first packable is a folder.
UnityEngine.Object[ ] objs = atlas.GetPackables();
UnityEditor.DefaultAsset asset = objs[0] as UnityEditor.DefaultAsset;
var path = AssetDatabase.GetAssetPath(asset);
@Venkify that’s not a complete answer, though - the sprite can be not directly in the mentioned folder, but in any of it’s descendants as well. I already implemented that check, but I must add that the current implementation of sprite atlas system lacks in usability.
private bool IsAtlasPacked(Sprite sprite)
{
var atlasesGUID = AssetDatabase.FindAssets("t:spriteatlas");
foreach (var atlasGUID in atlasesGUID)
{
SpriteAtlas atlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(AssetDatabase.GUIDToAssetPath(atlasGUID));
if (atlas.CanBindTo(sprite))
return true;
}
return false;
}
4 Likes