Skip to content

Commit

Permalink
Merge pull request #44 from nowsprinting/fix/gameview
Browse files Browse the repository at this point in the history
Fix open GameView on another window when using FocusGameView and GizmosShowOnGameView attributes
  • Loading branch information
nowsprinting committed Nov 1, 2023
2 parents 1730efb + 98ef199 commit 5b054e5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
9 changes: 9 additions & 0 deletions RuntimeInternals/GameViewControlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down
25 changes: 19 additions & 6 deletions Tests/Runtime/Attributes/FocusGameViewAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
[FocusGameView]
[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);
Expand All @@ -31,24 +32,36 @@ public void Attach_GameViewHasFocus()
}

[Test]
[FocusGameView]
[IgnoreWindowMode("For batchmode test.")]
[FocusGameView]
public void Attach_KeepBatchmode()
{
Assert.That(Application.isBatchMode, Is.True);
}

[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;
}
}
Expand Down
7 changes: 5 additions & 2 deletions Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace TestHelper.Attributes
{
[TestFixture]
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
public class GizmosShowOnGameViewAttributeTest
{
private class GizmoDemo : MonoBehaviour
Expand Down Expand Up @@ -54,18 +55,20 @@ public void Attach_False_HideGizmos()

[Test]
[CreateScene(camera: true, light: true)]
[GizmosShowOnGameView()]
[GizmosShowOnGameView]
public async Task AttachToAsyncTest_ShowGizmos()
{
await Task.Yield();
// verify screenshot.
}

[UnityTest]
[CreateScene(camera: true, light: true)]
[GizmosShowOnGameView()]
[GizmosShowOnGameView]
public IEnumerator AttachToUnityTest_ShowGizmos()
{
yield return null;
// verify screenshot.
}
}
}

0 comments on commit 5b054e5

Please sign in to comment.