GameViewOrientationEditorHook.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4. namespace AppUI
  5. {
  6. [InitializeOnLoad]
  7. static class GameViewOrientationEditorHook
  8. {
  9. const string MenuPath = "Tools/编辑器 Game 视图横竖屏 (1170×2532)";
  10. static GameViewOrientationEditorHook()
  11. {
  12. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  13. SceneManager.sceneLoaded += OnSceneLoaded;
  14. }
  15. static void OnPlayModeStateChanged(PlayModeStateChange state)
  16. {
  17. if (!ScreenOrientationEditorSettings.IsEnabled)
  18. return;
  19. switch (state)
  20. {
  21. case PlayModeStateChange.ExitingEditMode:
  22. // 进入 Play 前记录当前 Game 视图(下拉项 + 视口宽高)
  23. GameViewSizeHelper.CapturePrePlaySnapshot();
  24. break;
  25. case PlayModeStateChange.EnteredPlayMode:
  26. GameViewSizeHelper.ScheduleEnforce(
  27. GameViewSizeHelper.PortraitWidth,
  28. GameViewSizeHelper.PortraitHeight,
  29. 60);
  30. break;
  31. case PlayModeStateChange.EnteredEditMode:
  32. // 停止 Play 后恢复进入 Play 前的视图,而非固定 1170×2532
  33. GameViewSizeHelper.RestorePrePlaySnapshot();
  34. break;
  35. }
  36. }
  37. static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  38. {
  39. if (!EditorApplication.isPlaying || !ScreenOrientationEditorSettings.IsEnabled)
  40. return;
  41. if (scene.name == "Home")
  42. {
  43. GameViewSizeHelper.ScheduleEnforce(
  44. GameViewSizeHelper.LandscapeWidth,
  45. GameViewSizeHelper.LandscapeHeight,
  46. 90);
  47. }
  48. else if (scene.name == "Entry" || scene.name == "Login")
  49. {
  50. GameViewSizeHelper.ScheduleEnforce(
  51. GameViewSizeHelper.PortraitWidth,
  52. GameViewSizeHelper.PortraitHeight,
  53. 60);
  54. }
  55. }
  56. [MenuItem(MenuPath, false, 100)]
  57. static void ToggleAutoOrientation()
  58. {
  59. ScreenOrientationEditorSettings.IsEnabled = !ScreenOrientationEditorSettings.IsEnabled;
  60. Debug.Log(
  61. $"[AppUI] 编辑器 Game 视图自动横竖屏: " +
  62. $"{(ScreenOrientationEditorSettings.IsEnabled ? "开" : "关")}");
  63. }
  64. [MenuItem(MenuPath, true)]
  65. static bool ToggleAutoOrientationValidate()
  66. {
  67. Menu.SetChecked(MenuPath, ScreenOrientationEditorSettings.IsEnabled);
  68. return true;
  69. }
  70. }
  71. }