From 98ef19999911d2418af7d7a92bb6d2c21f0127f1 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Thu, 2 Nov 2023 01:46:04 +0900 Subject: [PATCH] Fix open GameView on another window when using Focus and Gizmos attributes --- RuntimeInternals/GameViewControlHelper.cs | 9 ++++++++ .../Attributes/FocusGameViewAttributeTest.cs | 21 +++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/RuntimeInternals/GameViewControlHelper.cs b/RuntimeInternals/GameViewControlHelper.cs index 859183f..ed5d44b 100644 --- a/RuntimeInternals/GameViewControlHelper.cs +++ b/RuntimeInternals/GameViewControlHelper.cs @@ -21,6 +21,9 @@ public static class GameViewControlHelper public static void Focus() { #if UNITY_EDITOR +#if UNITY_2022_2_OR_NEWER + PlayModeWindow.SetViewType(PlayModeWindow.PlayModeViewTypes.GameView); +#endif GameViewWrapper.GetWindow(); #endif } @@ -33,6 +36,9 @@ public static bool GetGizmos() { var gizmos = false; #if UNITY_EDITOR +#if UNITY_2022_2_OR_NEWER + PlayModeWindow.SetViewType(PlayModeWindow.PlayModeViewTypes.GameView); +#endif var gameViewWrapper = GameViewWrapper.GetWindow(false); if (gameViewWrapper != null) { @@ -49,6 +55,9 @@ public static bool GetGizmos() public static void SetGizmos(bool show) { #if UNITY_EDITOR +#if UNITY_2022_2_OR_NEWER + PlayModeWindow.SetViewType(PlayModeWindow.PlayModeViewTypes.GameView); +#endif var gameViewWrapper = GameViewWrapper.GetWindow(false); if (gameViewWrapper != null) { diff --git a/Tests/Runtime/Attributes/FocusGameViewAttributeTest.cs b/Tests/Runtime/Attributes/FocusGameViewAttributeTest.cs index 3150077..9fde962 100644 --- a/Tests/Runtime/Attributes/FocusGameViewAttributeTest.cs +++ b/Tests/Runtime/Attributes/FocusGameViewAttributeTest.cs @@ -16,13 +16,14 @@ namespace TestHelper.Attributes [UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)] public class FocusGameViewAttributeTest { + private const string GameView = "UnityEditor.GameView"; + private const string SimulatorWindow = "UnityEditor.DeviceSimulation.SimulatorWindow"; + [Test] [IgnoreBatchMode("Open GameView in batchmode but can not get window.")] [FocusGameView] public void Attach_GameViewHasFocus() { - const string GameView = "UnityEditor.GameView"; - const string SimulatorWindow = "UnityEditor.DeviceSimulation.SimulatorWindow"; #if UNITY_EDITOR var focusedWindow = EditorWindow.focusedWindow; Assert.That(focusedWindow, Is.Not.Null); @@ -39,16 +40,28 @@ public void Attach_KeepBatchmode() } [Test] + [IgnoreBatchMode("Open GameView in batchmode but can not get window.")] [FocusGameView] - public async Task AttachToAsyncTest_Normally() + public async Task AttachToAsyncTest_GameViewHasFocus() { +#if UNITY_EDITOR + var focusedWindow = EditorWindow.focusedWindow; + Assert.That(focusedWindow, Is.Not.Null); + Assert.That(focusedWindow.GetType().FullName, Is.EqualTo(GameView).Or.EqualTo(SimulatorWindow)); +#endif await Task.Yield(); } [UnityTest] + [IgnoreBatchMode("Open GameView in batchmode but can not get window.")] [FocusGameView] - public IEnumerator AttachToUnityTest_Normally() + public IEnumerator AttachToUnityTest_GameViewHasFocus() { +#if UNITY_EDITOR + var focusedWindow = EditorWindow.focusedWindow; + Assert.That(focusedWindow, Is.Not.Null); + Assert.That(focusedWindow.GetType().FullName, Is.EqualTo(GameView).Or.EqualTo(SimulatorWindow)); +#endif yield return null; } }