using UnityEditor; using UnityEngine; using UnityEngine.SceneManagement; namespace AppUI { [InitializeOnLoad] static class GameViewOrientationEditorHook { const string MenuPath = "Tools/编辑器 Game 视图横竖屏 (1170×2532)"; static GameViewOrientationEditorHook() { EditorApplication.playModeStateChanged += OnPlayModeStateChanged; SceneManager.sceneLoaded += OnSceneLoaded; } static void OnPlayModeStateChanged(PlayModeStateChange state) { if (!ScreenOrientationEditorSettings.IsEnabled) return; switch (state) { case PlayModeStateChange.ExitingEditMode: // 进入 Play 前记录当前 Game 视图(下拉项 + 视口宽高) GameViewSizeHelper.CapturePrePlaySnapshot(); break; case PlayModeStateChange.EnteredPlayMode: GameViewSizeHelper.ScheduleEnforce( GameViewSizeHelper.PortraitWidth, GameViewSizeHelper.PortraitHeight, 60); break; case PlayModeStateChange.EnteredEditMode: // 停止 Play 后恢复进入 Play 前的视图,而非固定 1170×2532 GameViewSizeHelper.RestorePrePlaySnapshot(); break; } } static void OnSceneLoaded(Scene scene, LoadSceneMode mode) { if (!EditorApplication.isPlaying || !ScreenOrientationEditorSettings.IsEnabled) return; if (scene.name == "Home") { GameViewSizeHelper.ScheduleEnforce( GameViewSizeHelper.LandscapeWidth, GameViewSizeHelper.LandscapeHeight, 90); } else if (scene.name == "Entry" || scene.name == "Login") { GameViewSizeHelper.ScheduleEnforce( GameViewSizeHelper.PortraitWidth, GameViewSizeHelper.PortraitHeight, 60); } } [MenuItem(MenuPath, false, 100)] static void ToggleAutoOrientation() { ScreenOrientationEditorSettings.IsEnabled = !ScreenOrientationEditorSettings.IsEnabled; Debug.Log( $"[AppUI] 编辑器 Game 视图自动横竖屏: " + $"{(ScreenOrientationEditorSettings.IsEnabled ? "开" : "关")}"); } [MenuItem(MenuPath, true)] static bool ToggleAutoOrientationValidate() { Menu.SetChecked(MenuPath, ScreenOrientationEditorSettings.IsEnabled); return true; } } }