Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Nanako · May 24, 2015 at 11:35 PM · mousemouseover

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Keseren · Jan 05, 2016 at 08:18 AM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
7

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
6

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.)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bigChris · May 01, 2020 at 03:32 PM 0
Share

That worked!

avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nanako · May 25, 2015 at 12:37 AM 0
Share

what? huh ? to what gameobject?

avatar image Nanako · May 25, 2015 at 01:24 AM 0
Share

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?

avatar image asperatology · May 25, 2015 at 01:35 AM 0
Share

Newbie here, I am also curious with adding the script to the game object. Do you mean the "game object" as the "game window"?

avatar image Hexer · May 25, 2015 at 01:42 AM 0
Share

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

avatar image Nanako · May 25, 2015 at 02:21 AM 0
Share

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

avatar image
0

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)") { }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JCUDOS · Feb 13, 2019 at 05:52 AM 0
Share

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

avatar image eiked JCUDOS · Feb 13, 2019 at 08:18 AM 0
Share

Try the approach I have described at the bottom of this question asked. mouseOverWindow might be deprecated, but I don't know for sure.

avatar image JCUDOS eiked · Feb 14, 2019 at 07:14 PM 0
Share

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." ?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges