Hi,
There is an annoying bug on the SpriteAtlas inspector, the sprite atlas preview is not showing at the bottom of the inspector.
The only way I found to see it is to change a setting, click on Pack Preview then rewind the settings has they need to be and re click on Pack Preview.
My project is using Unity 6.0.35 and SpriteAtlasV2.
On an other project using Unity 2022.3 and SpriteAtlas V1, there is no problem.
I sent a bug report about it: IN-94119
Are you sure it’s not hidden at the bottom?
Click on that double line if that’s the case
And make a screenshot of what your inspector shows there, it would help for sure
unfortunately there’s noting here:
The bug appear when using SpriteAtlas V2 and the SpriteAtlasMode is not set to SpriteAtlasV2 - Enabled.
So when it’s set to BuildOnly, you can’t see your atlases xD.
Please note that SpriteAtlasMode should be set to SpriteAtlasV2 - Enabled if previews need to be seen at all times.
SpriteAtlasV2 - Enabled always packs SpriteAtlas whenever there is a change.
BuildOnly Mode only packs/builds SpriteAtlas on Building Players/AssetBundles/etc…
This is a regression from the behavior of the SpriteAtlas V1. In V1 it was possible to see SpriteAtlas preview once packed even if we are in BuildOnly mode or Disabled.
On my game I have lot of atlas and 4 variants of each.
When SpriteAtlas is enabled every time an asset is modified, a sprite packing occurs, ant it can be pretty long… Most of times I make changes but don’t want to re-trigger a sprite pack directly, I want to be able to change multiple files one by one and then once finished trigger the sprite packing.
So I made a script that set the Sprite Packing to BuildOnly when not playing and to Enabled when I start playing, so it trigger the sprite packing when entering play mode if needed. (Like with SpriteAtlasV1)
This way I can work on my sprite files without loosing time with sprite packing.
But now I have this problem that I can not see my packed sprites. Why can I see them in V1 but not in v2 ? If they are packed, just show them. I can understand to not show them if it’s set to Disabled, but “Build Time Only” means I use/need it no ?
Sprite Atlas V2 differs from V1 in that the packed textures are now stored in AssetDatabase (instead of a custom AtlasCache folder) and imported data is consistent with the settings in Editor properties. There are quite a few advantages using AssetDatabase.
Both V1 and V2 do not pack SpriteAtlas even if there is a change in the data unless PackPreview is invoked.
However in V1 since a custom cache folder is used, preview was displayed from the last packed state, not the active state. In V2, this is controlled by AssetDatabase and preview is consistent with the settings of ImportMode (Enabled vs BuildOnly).
If you would like to do the same for V2, you may want to write a script to toggle between the Enabled and BuildOnly state ? (because you are packing them before entering playmode anyway). Please let us know if this helps.
No, the problem is that with V2, the sprite packing is fired directly after an asset change instead of waiting to enter playmode (as it was with v1). So if you want to change for example the pivot of 50 sprites (that can’t be done in one time as the sprite editor doesn’t work with multiple selection), you will have 50 sprite packing… This is the same when you add files in the project, rename files, move files…
I Understand the benefits for the V2, but you have to admit that this behavior is a waste of time.
So I think that in BuildOnly, the preview should be shown.
Or at least make the v2 work like the v1 and don’t fire the sprite packing at every change.
In case someone come here with the same problem, here is a alternative solution working with Unity 6000.0.38f1.
Add this script to an Editor folder, then right click a SpriteAtlas > Show Sprite Atlas Preview.
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 );
}
}
}
For anyone encountering this issue. The problem is - you can drag anything into the “Objects for Packing” and it will not show any warnings or errors but the preview works ONLY for textures that are set up as:
- Texture Type: Sprite (2D and UI)
This behavior of automatically creating an atlas every time a sprite is modified is extremely frustrating. For example, if you modify 30 images at once, it repacks the atlas 30 times, even though all 30 images are in the same atlas. This makes no sense at all.
I also tried using a script that activates and deactivates the atlas when entering and exiting Play mode, but it doesn’t always work. Could you share that script with me? My version sometimes doesn’t pack the atlas when I enter Play mode, or sometimes it doesn’t deactivate when I exit Play mode. I’m not sure why this happens.
Hi @MichelVictor sure here is the script I use to set the SpriteAtlasMode to buildOnly when not in play mode:
Put in in an Editor folder and toggle Tools/SpriteAtlas AutoEnable/Enabled to enable it.
using UnityEditor;
using UnityEngine;
public static class SpriteAtlasAutoEnable
{
private const string cEditorPrefSpriteAtlasAutoEnable = "SpriteAtlasAutoEnable.AutoEnable";
private const string menuName = "Tools/SpriteAtlas AutoEnable/Enabled";
public static bool AutoEnable
{
get => EditorPrefs.GetBool( cEditorPrefSpriteAtlasAutoEnable, false );
set
{
if( value != AutoEnable )
{
EditorPrefs.SetBool( cEditorPrefSpriteAtlasAutoEnable, value );
if( value )
EditorSettings.spritePackerMode = EditorApplication.isPlayingOrWillChangePlaymode ? SpritePackerMode.SpriteAtlasV2 : SpritePackerMode.SpriteAtlasV2Build;
Menu.SetChecked(menuName, value);
}
}
}
[InitializeOnLoadMethod]
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void RuntimeInitializeOnLoadMethod()
{
EditorApplication.playModeStateChanged -= OnEnterPlayMode;
EditorApplication.playModeStateChanged += OnEnterPlayMode;
if( AutoEnable )
EditorSettings.spritePackerMode = EditorApplication.isPlayingOrWillChangePlaymode ? SpritePackerMode.SpriteAtlasV2 : SpritePackerMode.SpriteAtlasV2Build;
Menu.SetChecked(menuName, AutoEnable);
}
private static void OnEnterPlayMode(PlayModeStateChange obj)
{
if( !AutoEnable )
return;
EditorSettings.spritePackerMode = obj switch
{
PlayModeStateChange.ExitingEditMode => SpritePackerMode.SpriteAtlasV2,
PlayModeStateChange.ExitingPlayMode => SpritePackerMode.SpriteAtlasV2Build,
_ => EditorSettings.spritePackerMode
};
}
[MenuItem( menuName )]
private static void ToggleEnabled()
{
AutoEnable = !AutoEnable;
Menu.SetChecked(menuName, AutoEnable);
}
}
Great! It’s working perfectly! Thank you!
Same here. This problem is still happening in Unity 6.1.6f1 and 6.2.12f1
Sprite Atlas V2 - Enabled : re-packs all atlases every time an asset is changed, impacting work flow.
This is different behaviour than how it worked in Unity 2022.3 (and no, it’s not in comparison to V1)
In my case, solution is simple.
If you get sprites created by other design programs, you need to check sprite option.
‘Sprite Mode’ in sprite option in inspector must be ‘Single’.