Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by jkeiser · Feb 06, 2016 at 02:38 PM · cursoronmouseenterinput.mousepositioncursor-customizationonmouseexit

How to detect mouse leaving / entering the game window?

Hi!

How can I tell when the mouse has left the window? Past answers have led me to:

  1. OnMouseEnter / OnMouseExit, which appears to be limited UI elements with a specific width and height. I am not sure what element I'd attach this to--is there an element somewhere representing the screen?

     // What object would I attach this to?
     void OnMouseEnter()
     {
         Cursor.visible = true;
     }
     void OnMouseExit()
     {
         Cursor.visible = false;
     }
    
    
  2. Bounds checking on Update, which is failing me because Unity clips mousePosition to be inside the bounds of the window at all times (even if the mouse is outside). The code for this one:

     void Update ()
     {
         // Disable the hardware cursor while the mouse is inside the window
         Rect screenRect = new Rect(0, 0, Screen.width, Screen.height);
         Cursor.visible = screenRect.Contains(Input.mousePosition);
     }
    

The problem I'm trying to solve: I want a software cursor I control, so I want to disable the hardware cursor while the mouse is inside my window, and reenable when it exits. It's very difficult to work in Unity when the cursor is disabled :)

Thanks!

--John

Comment
Add comment · Show 3
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 meat5000 ♦ · Feb 06, 2016 at 02:38 PM 0
Share

Guessing you saw this one :

http://answers.unity3d.com/questions/474825/how-to-restrict-mouse-going-off-screen.html

avatar image jkeiser · Feb 06, 2016 at 09:02 PM 0
Share

Yep, that one advocates using mouse position, which isn't working for me due to the fact that that position is never given a value out of bounds--it is clipped to window width and height.

avatar image Teravisor · Feb 06, 2016 at 11:05 PM 0
Share

Unfortunately, from what I've searched for, Unity doesn't have such option just like it doesn't have any event that triggers when window is being moved(causes game to freeze without any warnings - especially bad in multiplayer), resized(makes us check Screen.width/height in Update) and such. All of them are OS based events.

Didn't test it, but maybe you can lock cursor to screen and unlock it when it's on edge (or somewhere near edge) simulating needed behaviour? And locking back when it's inside again.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by eviljake29 · Feb 07, 2016 at 05:55 PM

Right click in the hierarchy and create an empty

Attach the first script to that

  void OnMouseEnter()
  {
      Cursor.visible = false;
  }
  void OnMouseExit()
  {
      Cursor.visible = true;
  }

And you have to switch the Boolean values around...

before it turned the cursor on when it entered the window.

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

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

6 People are following this question.

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

Related Questions

Change cursor on OnTrigger 0 Answers

Keep Cursor at center and project interactions to cursor position on mobile touch devices (IOS) 0 Answers

How do I change the cursor without a separate Texture2D? 0 Answers

RenderTexture and OnMouseEnter offset 0 Answers

Glue mouse to object 2 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