diff --git a/Source/Editor/Editor.cs b/Source/Editor/Editor.cs
index 75ee0b481..fc6d72ee9 100644
--- a/Source/Editor/Editor.cs
+++ b/Source/Editor/Editor.cs
@@ -1301,6 +1301,17 @@ namespace FlaxEditor
return false;
}
+ internal void Internal_FocusGameViewport()
+ {
+ if (Windows.GameWin != null)
+ {
+ if (StateMachine.IsPlayMode && !StateMachine.PlayingState.IsPaused)
+ {
+ Windows.GameWin.FocusGameViewport();
+ }
+ }
+ }
+
internal void Internal_ScreenToGameViewport(ref Float2 pos)
{
var win = Windows.GameWin?.Root;
diff --git a/Source/Editor/Managed/ManagedEditor.cpp b/Source/Editor/Managed/ManagedEditor.cpp
index 3f0c613e8..c33e2c63e 100644
--- a/Source/Editor/Managed/ManagedEditor.cpp
+++ b/Source/Editor/Managed/ManagedEditor.cpp
@@ -27,6 +27,7 @@ MMethod* Internal_LightmapsBake = nullptr;
MMethod* Internal_CanReloadScripts = nullptr;
MMethod* Internal_CanAutoBuildCSG = nullptr;
MMethod* Internal_CanAutoBuildNavMesh = nullptr;
+MMethod* Internal_FocusGameViewport = nullptr;
MMethod* Internal_HasGameViewportFocus = nullptr;
MMethod* Internal_ScreenToGameViewport = nullptr;
MMethod* Internal_GameViewportToScreen = nullptr;
@@ -372,6 +373,19 @@ bool ManagedEditor::HasGameViewportFocus() const
return result;
}
+void ManagedEditor::FocusGameViewport() const
+{
+ if (HasManagedInstance())
+ {
+ if (Internal_FocusGameViewport == nullptr)
+ {
+ Internal_FocusGameViewport = GetClass()->GetMethod("Internal_FocusGameViewport");
+ ASSERT(Internal_FocusGameViewport);
+ }
+ Internal_FocusGameViewport->Invoke(GetManagedInstance(), nullptr, nullptr);
+ }
+}
+
Float2 ManagedEditor::ScreenToGameViewport(const Float2& screenPos) const
{
Float2 result = screenPos;
diff --git a/Source/Editor/Managed/ManagedEditor.h b/Source/Editor/Managed/ManagedEditor.h
index c08c17e3f..8d6547377 100644
--- a/Source/Editor/Managed/ManagedEditor.h
+++ b/Source/Editor/Managed/ManagedEditor.h
@@ -100,6 +100,11 @@ public:
/// True if game viewport is focused, otherwise false.
bool HasGameViewportFocus() const;
+ ///
+ /// Gives focus to the game viewport (game can receive input).
+ ///
+ void FocusGameViewport() const;
+
///
/// Converts the screen-space position to the game viewport position.
///
diff --git a/Source/Editor/Modules/SimulationModule.cs b/Source/Editor/Modules/SimulationModule.cs
index 0994ce9f0..07a67714a 100644
--- a/Source/Editor/Modules/SimulationModule.cs
+++ b/Source/Editor/Modules/SimulationModule.cs
@@ -191,16 +191,7 @@ namespace FlaxEditor.Modules
// Show Game widow if hidden
if (gameWin != null && gameWin.FocusOnPlay)
{
- if (!gameWin.IsDocked)
- {
- gameWin.ShowFloating();
- }
- else if (!gameWin.IsSelected)
- {
- gameWin.SelectTab(false);
- gameWin.RootWindow?.Window?.Focus();
- FlaxEngine.GUI.RootControl.GameRoot.Focus();
- }
+ gameWin.FocusGameViewport();
}
Editor.Log("[PlayMode] Enter");
diff --git a/Source/Editor/Windows/GameWindow.cs b/Source/Editor/Windows/GameWindow.cs
index f5d6c8d45..4f9a18218 100644
--- a/Source/Editor/Windows/GameWindow.cs
+++ b/Source/Editor/Windows/GameWindow.cs
@@ -471,6 +471,23 @@ namespace FlaxEditor.Windows
}
}
+ ///
+ /// Focuses the game viewport. Shows the window if hidden or unselected.
+ ///
+ public void FocusGameViewport()
+ {
+ if (!IsDocked)
+ {
+ ShowFloating();
+ }
+ else if (!IsSelected)
+ {
+ SelectTab(false);
+ RootWindow?.Window?.Focus();
+ }
+ Focus();
+ }
+
///
/// Takes the screenshot of the current viewport.
///
diff --git a/Source/Engine/Engine/Engine.cpp b/Source/Engine/Engine/Engine.cpp
index ae1fa0563..cf24223ea 100644
--- a/Source/Engine/Engine/Engine.cpp
+++ b/Source/Engine/Engine/Engine.cpp
@@ -409,6 +409,19 @@ JsonAsset* Engine::GetCustomSettings(const StringView& key)
return Content::LoadAsync(assetId);
}
+void Engine::FocusGameViewport()
+{
+#if USE_EDITOR
+ Editor::Managed->FocusGameViewport();
+#else
+ if (MainWindow)
+ {
+ MainWindow->BringToFront();
+ MainWindow->Focus();
+ }
+#endif
+}
+
bool Engine::HasGameViewportFocus()
{
#if USE_EDITOR
diff --git a/Source/Engine/Engine/Engine.h b/Source/Engine/Engine/Engine.h
index 7fc5430f2..0e72e6b4b 100644
--- a/Source/Engine/Engine/Engine.h
+++ b/Source/Engine/Engine/Engine.h
@@ -153,6 +153,11 @@ public:
static Window* MainWindow;
+ ///
+ /// Brings focused to the game viewport (game can receive input).
+ ///
+ API_FUNCTION() static void FocusGameViewport();
+
///
/// Checks whenever the game viewport is focused by the user (eg. can receive input).
///