Skip to content

Instantly share code, notes, and snippets.

@binouze
Created November 22, 2024 09:06
Show Gist options
  • Save binouze/ca077aa1c2d2e8b4b43241246ab78190 to your computer and use it in GitHub Desktop.
Save binouze/ca077aa1c2d2e8b4b43241246ab78190 to your computer and use it in GitHub Desktop.
Enabling Unity's SpriteAtlasV2 only when entering play mode to prevent constant sprite packing.
using UnityEditor;
[InitializeOnLoad]
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, true );
set
{
EditorPrefs.SetBool( cEditorPrefSpriteAtlasAutoEnable, value );
if( value )
EditorSettings.spritePackerMode = EditorApplication.isPlayingOrWillChangePlaymode ? SpritePackerMode.SpriteAtlasV2 : SpritePackerMode.SpriteAtlasV2Build;
Menu.SetChecked(menuName, value);
}
}
static SpriteAtlasAutoEnable()
{
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);
}
}
@binouze
Copy link
Author

binouze commented Nov 22, 2024

For those like me that are annoyed by constant sprite packing since the switch to spriteatlasv2, here is a script that, when enabled, set the sprite packing mode to "Build Only" then switch to "Always" when entering play mode. This will mimics the old spriteatlas behavior by sprite packing only when entering play mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment