- Home /
Is there a way to detect when the game window has focus?
I'd like to know when my game window has focus, and is capturing inputs.
Just spent a minute perplexed why my script wasnt reading the mousehweel movement, before realising i had to click in it. I'd like to add a visible indicator/error message to let me know when its not focused.
how?
Answer by HarshadK · Apr 21, 2015 at 10:55 AM
To know this from your script you can use: MonoBehaviour.OnApplicationFocus(bool)
hooray. This works
having it as a seperate function/monobehaviour is mildly annoying, is there any more compact method? like a function call, or a variable to check/ is i necessarf to maintain that myself?
You can actually do this yourself.
Just create a script with a singleton pattern that sets a variable to true or false based on the state of focus on game window. You can now check this variable to see if the game window has focus or not from any other script.
yes, i'm well aware that i can do that. I'm just asking if it's already done anywhere internally. This seems like a variable that the application would want to keep track of naturally, no?
There is one such variable but it is for editor window and it returns the window with keyboard focus: EditorWindow.focusedWindow
I do not know of any variable that is exposed in public API for OnApplicationFocus. :-(
Your answer
Follow this Question
Related Questions
How to distinguish Firebase Push Notification received in Background and when App open 0 Answers
Keeping Unity Focused 0 Answers
Problem with Unity GUI ! 0 Answers
C# Tooltips through multiple windows 1 Answer
Remove Focus From InputField 2 Answers