Created
November 22, 2024 09:06
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.