Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by grumpntug · May 12, 2018 at 01:09 AM · cursorfocus

Cursor lock after regaining focus - Unity 2018.1.0f2

I have an in-game menu that I load when pressing escape that pauses the game, makes the cursor visible, and sets its restriction to none so users can freely click in other windows outside of the game if need be. It also auto pauses when the window loses focus.

When you press escape or the resume button, the cursor locks again and goes invisible.

Everything behaves as I want except when the game window loses focus. Once I regain focus back into the game window and resume the game the cursor is hidden but not locked. The game still works as usual, but I can see the cursor leave the window while looking around which, if you click on something in the game, will register as a click outside the window losing focus. For some reason, my cursor lock works fine in every case up until I lose focus and regain focus. I'm not sure what I'm missing. The cursor code is pretty straight forward.

             Cursor.visible = !Cursor.visible;
             //If the options menu has been closed, lock the mouse. Otherwise, unlock it
             if (optionsPanelToggle == false)
             {
                 Cursor.lockState = CursorLockMode.Locked;
             }
             else
             {
                 Cursor.lockState = CursorLockMode.None;
             }

This locks the cursor in place when I close the menu. However, after focus is lost then re-gained, when I close the menu it doesn't lock.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by grumpntug · May 13, 2018 at 05:15 AM

I seem to have found a solution Basically when you lose focus of the program, windows sort of takes over cursor behavior again and Unity's cursor lock doesn't actually regain control after that. So I found you basically have to set the cursor position yourself after regaining focus.


I ended up working with a free unity asset that lets you read and set the cursor value for both local and global cursor positions in windows. I didn't want to import a windows dll and basically build cursor tracking from scratch just so I could set the position and this was free.


Cursor Control


So what I ended up doing was recording the global cursor position once the player clicks the resume button in the pause menu. This makes sure the cursor is within the game window without extra code to detect it and the player can relocated the window to any display location across multiple displays.


It feels a bit rough doing it this way, but it's the best solution I have found so far. After I get the position on resume, I throw that cursor location in update to keep it there. Everything works correctly so far.

  Vector2 _cursorForcePos;
 
 //If the game has been resumed, set time and record the cursor's global position
 
         if (optionsPanelToggle == false)
         {
             _cursorForcePos = CursorControl.GetGlobalCursorPos();
             Time.timeScale = 1;
         }
 
     private void Update()
     {
 
 // Check if the player is using a terminal and if the pause menu has been deactivated.
 
         if (CamManager.Ins.terminalUse == false && optionsPanelToggle == false)
         {
 
             CursorControl.SetGlobalCursorPos(_cursorForcePos);
         }
 }


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

140 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 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 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 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 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

Mouse Pointer disappears 2 Answers

Cursor.visible not working? 3 Answers

How to make a similar cursor like the one in below image ? 0 Answers

cursor cannot active and deactive when click ui 0 Answers

Mouse cursor lag in editor 1 Answer


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