- Home /
How can i tell if the mouse is over the game window?
Pretty simple question. How can i tell if the current position of the mouse cursor is hovering over the game window, whether in the editor or running standalone
Yes, I'm wondering the same thing. Game Window, as in the window you get by pressing Ctrl2, the window you're working at, not a gameobject as Hexer thought.
Answer by zet23t · Feb 13, 2018 at 03:56 PM
Another approach that works just with the mouse position is to check if it's outside the viewport of the current camera:
var view = camera.ScreenToViewportPoint(Input.mousePosition);
var isOutside = view.x < 0 || view.x > 1 || view.y < 0 || view.y > 1;
Answer by LW · Dec 03, 2019 at 08:52 PM
Came across this question and if anyone else comes here the simple answer is:
bool IsMouseOverGameWindow { get { return !(0 > Input.mousePosition.x || 0 > Input.mousePosition.y || Screen.width < Input.mousePosition.x || Screen.height < Input.mousePosition.y); } }
(Works in standalone builds and in editor for just the Game View.)
Answer by Hexer · May 24, 2015 at 11:52 PM
Void OnMouseEnter (){
Debug.Log("OMG its inside the Collider");
}
be sure to add an collider to the gameobject.
You can change OnMouseEnter to OnMouseOver if you want to check it every frame or use an combination of OnMouseEnter and OnMouseExit.
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html
how can i add a script to an editor window? And a collider? this makes no sense.
isn't there a way to do it with the input or application classes?
Newbie here, I am also curious with adding the script to the game object. Do you mean the "game object" as the "game window"?
Well you can do as I said and make the collider as big as the screen area itself. When the mouse leaves the area it gives you a Debug.Log in the console. But I don't understand why you want to check if the mouse cursor is hovering over the game scene. Sorry
hmm, you can't guess? I'd have thought that would be pretty obvious.
I want various controls (like the mouse wheel scrolling, or clicks, or keyboard input) to only work when the cursor is over the game window. This is a pretty common game feature
Answer by Keseren · Jan 05, 2016 at 08:17 AM
Found it by luck! Use the feature (mouseOverWindow). For example Debug.Log(mouseOverWindow); will say UnityEditor.GameView when you're hovering over the game window
Example:
if (mouseOverWindow.ToString() == " (UnityEditor.GameView)") { }
Is this in C# ? If not, is there a way to do it in C#? If it is already C#, I don't understand why I can't get it to work. I tried with using UnityEngine.UI; using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEditor; and a few others and I'm always getting : The name 'mouseOverWindow' does not exist in the current context
Try the approach I have described at the bottom of this question asked. mouseOverWindow might be deprecated, but I don't know for sure.
Thanks but I don't see any other comment by you on this question. What do you mean by "the approach I have described at the bottom of this question asked." ?
Your answer
Follow this Question
Related Questions
How to implement glow when moused over? 1 Answer
2D OnMouseOver issues 0 Answers
change mouse cursor on mouseover 3 Answers
Problem with OnMouseDown(not registering clicks in some situations) 0 Answers
Setting ToolTip to Above Mouse Position 5 Answers