Is there a way to preview sprite atlases in editor when using "Sprite Atlas v2 Enabled for Builds"?

I recently upgraded my project to Unity 2022 and switched from Sprite Atlas V1 to Sprite Atlas V2. In Sprite Atlas V1, I used the “Enabled for Builds” option because sprite packing took too long. However, if I try to use the same option in Sprite Atlas V2, I can’t preview the sprite atlases anymore. There is no preview in the inspector and I’m missing the bar that you can tap on to reveal the preview. This feature was very useful to me, so I’m disappointed that it seems to be missing with the “Enabled for Builds” option. I don’t have this problem with the “Always Enabled” option, but I’d rather not turn this on and force everyone on the team to wait for sprite packing - which seems improved in V2, but it would still be nice to have the same features we had in V1. Seeing the packed atlas helps me optimize sprites. Is this a bug or is there some other way to preview the atlases?

This is not a fix but a workaround - if you change any of the options (eg check read / write) you can press the pack preview button. You’ll want to revert the change after. As for your preview window missing, that’s a separate bug, I believe if you middle mouse click on the asset labels bar it will appear lol

I am also having this problem. I am unable to generate a preview for the packed sprites. The “Pack Preview” button just greys itself out with no helpful information delivered. Given how quickly I ran into this problem, and the lack of further responses echoing your concerns, I can only assume that this will continue to be a problem for years, and you might want to seek out third party tools for packing these. That’s what I’ve been searching for, in lieu of a useful Unity method for packing sprites.

2 Likes

I submitted a bug report about this some weeks ago and received the answer that this is not a bug but a feature…

he is the issue tracker link for informations: Unity Issue Tracker - Sprite Atlas Inspector preview disappears when entering Play mode unless SpriteAtlasMode is set to "Sprite Atlas V2 - Enabled"

In case anyone is interested, here is an editor window to show sprite atlas preview even if spritePackerMode is set to SpriteAtlasV2Build.

Note that I tested it with Unity 6 so there may be some changes to have it working on earlier versions.

using UnityEditor;
using UnityEngine;
using System.Reflection;
using UnityEditor.U2D;
using UnityEngine.U2D;

public class SpriteAtlasPreview : EditorWindow
{
    private SpriteAtlas selectedSpriteAtlas;
    private Texture2D[] previewTextures;
    private string[]    previewNames;
    private int         selectedPreviewIndex;

    [MenuItem( "Tools/Show Sprite Atlas Preview")]
    [MenuItem( "Assets/Show Sprite Atlas Preview")]
    public static void ShowWindow()
    {
        GetWindow<SpriteAtlasPreview>("Sprite Atlas Preview");
    }
    [MenuItem( "Tools/Show Sprite Atlas Preview",true)]
    [MenuItem( "Assets/Show Sprite Atlas Preview",true)]
    public static bool _ShowWindow()
    {
        return Selection.activeObject is SpriteAtlas; // disable menu item if the selection is not a SpriteAtlas
    }
    
    private void OnEnable()
    {
        EditorApplication.update += OnEditorUpdate;
    }
    private void OnDisable()
    {
        EditorApplication.update -= OnEditorUpdate;
    }
    private void OnDestroy()
    {
        EditorApplication.update -= OnEditorUpdate;
    }

    private bool triedPacking;
    private void OnEditorUpdate()
    {
        // check if selection changed
        if( Selection.activeObject != selectedSpriteAtlas )
        {
            selectedSpriteAtlas  = Selection.activeObject as SpriteAtlas;
            selectedPreviewIndex = 0;
            previewNames         = null;

            if( selectedSpriteAtlas != null )
            {
                titleContent = new GUIContent("Sprite Atlas Preview - " + selectedSpriteAtlas.name);
                
                // get sprite atlas preview using reflexion as the function is internal
                var getPreviewTexturesMethod = typeof(SpriteAtlasExtensions).GetMethod("GetPreviewTextures", BindingFlags.Static | BindingFlags.NonPublic);
                if( getPreviewTexturesMethod != null )
                {
                    previewTextures = (Texture2D[])getPreviewTexturesMethod.Invoke(null, new object[] { selectedSpriteAtlas });
                }

                if( (previewTextures == null || previewTextures.Length == 0) && !triedPacking )
                {
                    triedPacking = true;
                    // starting a pack preview unlock the display of the preview even if the spritePackerMode is set to BuildOnly,
                    // else the preview is maybe d'ont available because it needs to be packed ... so let's pack it!
                    SpriteAtlasUtility.PackAtlases( new []{selectedSpriteAtlas},EditorUserBuildSettings.activeBuildTarget );
                    SpriteAtlasUtility.CleanupAtlasPacking();
                    // set it to null to reenter the function and retry get the previews
                    selectedSpriteAtlas = null; 
                }

                if( previewTextures is { Length: > 0 } )
                {
                    // create the values for the dropdown
                    previewNames = new string[previewTextures.Length];
                    for( var i = 0; i < previewTextures.Length; i++ )
                        previewNames[i] = "MainTex - Page ("+(i + 1)+")";
                }
            }
            else
            {
                titleContent         = new GUIContent("Sprite Atlas Preview");
                previewTextures      = null;
                selectedPreviewIndex = 0;
            }

            // force repaint
            Repaint();
        }
    }
    
    private void OnGUI()
    {
        if( selectedSpriteAtlas == null )
        {
            EditorGUILayout.LabelField("No Sprite Atlas selected.");
            return;
        }
        
        if( previewTextures == null || previewTextures.Length == 0 )
        {
            EditorGUILayout.LabelField("No preview textures found.");

            if( GUILayout.Button("Pack Preview") )
            {
                SpriteAtlasUtility.PackAtlases( new []{selectedSpriteAtlas},EditorUserBuildSettings.activeBuildTarget );
                SpriteAtlasUtility.CleanupAtlasPacking();
                selectedSpriteAtlas = null;
            }
            
            return;
        }

        // show the dropdown to select the texture to preview
        selectedPreviewIndex = EditorGUILayout.Popup($"Preview ({previewTextures.Length})", selectedPreviewIndex, previewNames);

        // display the selected preview
        var selectedPreview = previewTextures[selectedPreviewIndex];
        if( selectedPreview != null )
        {
            // calculate a zone to display the image with some padding
            var top    = EditorGUIUtility.singleLineHeight * 2;
            var height = position.height - (EditorGUIUtility.singleLineHeight*3);
            var left   = EditorGUIUtility.singleLineHeight;
            var width  = position.width - (EditorGUIUtility.singleLineHeight*2);
            var rect   = new Rect(left, top, width, height);
            
            // draw the texture with a checkboard behind
            EditorGUI.DrawTextureTransparent( rect, selectedPreview, ScaleMode.ScaleToFit );
        }
    }
}

2 Likes

Question, if the packing cannot be previewed does that mean they are not packed? I dont give a crap if I can see them in preview window, I just want to know they are packed. As well it seems if you change a texture normal to sprite 2d sprite atlas v2 wont accept it and reverts the texture back to normal texture. Is that normal? Im trying to pack textures for vegetation studio pro which program states can use sprite atlas v2. However, in order to see packing preview textures have to be changed to sprite 2d. But when I do this and try to load them in vegestudiopro the textures and normals revert back to normal textures out of sprite 2d. Actually they make a copy of the the sprite 2d texture as a normal texture in their originating folder. Docs online state vegetation studio pro cant use sprite 2d so Im confused. Do I just load them as normal textures without changing them to sprite 2d so I cant see a preview to know if they are packed and on a prayer hope their packed and working?

1 Like

Has this been fixed yet? It used to show the preview in Unity versions older than 6. It was extremely useful. Now it shows it after packing and then both the preview and Pack Preview button disappears as soon as I interact with anything else.

Happy to report I see it’s fixed in 6.3.10f1