Search code examples
c#unity-game-engineinputgamepad

How to avoid input detection when the game window isn't focused with Unity Input System?


I'm using Unity 2020.1.3f and Input System 1.0.2 to create a simple racing multiplayer game with MLAPI 0.1.0 but whenever i start an instance of the game after building it and unfocus the window it still detecting my gamepad input (with keyboard does not happen), but when I play the game in Unity's game view and unfocus the editor it does not detect the input anymore. What can i do to only detect my gamepad input when the window is focused?


Solution

  • You could either detect loss of focus using the Unity Event function OnApplicationFocus(bool) (Docs) or by checking the current focus state with Application.isFocused (Docs). Then in your input logic ignore gamepad input if focus is false