Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 eheimburg · May 10, 2011 at 04:40 AM · cursorpauseruninbackgroundonapplicationfocus

Mouse Cursor Hidden, Won't Come Back on Pause/Focus

It's easy enough to hide the mouse cursor (Screen.showCursor = false) so you can draw your own. BUT if you do this, how do you bring the cursor BACK when your game loses focus? (For instance in Windows, if the user Alt+Tabs to another window.)

Your script's OnApplicationFocus() and OnApplicationPause() functions DO get called. But setting Screen.showCursor = true here DOESN'T work. I think this is because Screen.showCursor isn't checked by Unity until the next screen refresh, and you don't get any more screen refreshes because your game becomes paused.

Is the only work-around to force your application to run in the background at all times (with Application.runInBackground = true)? If so, that sucks... it's very inefficient, even when I "pause" my game in software. I want my game to take 0% CPU when the user isn't playing it!

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

3 Replies

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

Answer by gtjuggler · Sep 21, 2011 at 06:38 PM

This works for me..

 void OnApplicationFocus(bool focus) {
     if(focus)
         StartCoroutine(LockCursor());    
 }
 
 IEnumerator LockCursor(){
     yield return new WaitForSeconds(0.1f);
     Screen.lockCursor = true;
 }
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 eheimburg · Aug 23, 2012 at 03:27 AM 0
Share

This is clever! Not sure why that works (you'd think the coroutine would be paused), but thanks, switching this to the correct answer

avatar image
2

Answer by IPTN · Jul 07, 2011 at 06:52 PM

Doh just ignore this first part.

Actually I can think of one way to do this however it would very much suck in another way. Basically instead of using Screen.showCursor() you would use Screen.lockCursor(). However that means you would have to do a lot of changes to the rest of the game if you need the user to be able to click on different places on the screen other than the center. For instance you would set up the custom cursor to move on the screen based on the mouse movement x and y axis from the input manager. Then you would need a script running that will always catch any mouse clicks ex:empty game object with a script that starts a coroutine that each frame checks to see if the user has clicked the mouse. If the mouse was clicked then check the position of the cursor against rect.Contains(CursorPosition) of all gui elements. For Game objects I'm not entirely sure how you would go about checking to see if one was underneath the cursor however I'm fairly certain that there is some way of doing it (didn't really look).

So in other words yes you could do what you are trying to do, but it would pretty much make the mouse either useless or way to much of a hassle/and or add more overhead to make it worthwhile.

SECOND PART:

Just thought of this while writing everything up there ^^ (Leaving it in case someone wants a good laugh) but what if you attached this script to an empty gameobject:

 Screen.showCursor = false;
 Application.runInBackground = true;
 private var ready : boolean = false;
 
 function Start(){
     //Normal Start code here
     ready = true;
 }
 
 function OnApplicationFocus(focus : boolean){
     if(ready){ //The reason for the ready is because I was getting this wierd double false call at the beginning
         Screen.showCursor = !focus;
         Application.runInBackground = focus;
     }
 }

NeverMind

ust tried it and continues to run in the background even after setting

Application.runInBackground = false;

(Leaving this here for reference)

Wow did I just get my butt kicked.

Anyone else because:

"there is no work-around apparently."

Eric 3

Edit

OK seems to be a mute point because if the game loses focus to another window the cursor will appear as long as you don't hover over the game. Move your cursor anywhere else on screen and it reappears. Soooo....

(Another interesting fact. Setting Application.runInBackround = false does not stop all processing for the game when it loses focus to another window. [at least not in editor mode])

Me = Idiot :)

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 J3-Gaming · Aug 23, 2012 at 05:06 AM 0
Share

Im going to give you a thumbs up just for the effort displayed. $$anonymous$$any members here would love this amount of help on questions they have.

avatar image
0

Answer by eheimburg · Jun 19, 2011 at 08:27 AM

To close my own question, no there is no work-around apparently.

In order to change the mouse cursor in OnApplicationPause() you must set Application.runInBackground = true. Which blows.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Screen.showCursor = false; Depends on X coordinate of mouse. 1 Answer

How to make custom cursors work without enabling "Run in Background"? 1 Answer

Cursor not shown after changing scene 1 Answer

Script that makes the mouse cursor invisible. 3 Answers

Cursor help 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