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!

This works for me…

void OnApplicationFocus(bool focus) {
	if(focus)
		StartCoroutine(LockCursor());	
}

IEnumerator LockCursor(){
	yield return new WaitForSeconds(0.1f);
	Screen.lockCursor = true;
}

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

Just 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 :slight_smile:

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.