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); } }