For all Unity developers and developers-to-be, both beginners and professionals!
How to hide the default system cursor in-game when it's not needed (in some platformers, etc.).
Posted by feillyne on Oct 24th, 2010
It's rather simple.
1. Create a script that and name it e.g. HideCursorScript, then edit it and paste the following code:
2. Attach your script to a game object, whether your character or simply the main camera - by dragging the script onto the object.
3. Then, to display the cursor, e.g. when pressing ESCAPE key to see the pause menu, you may use the code below:
The full code that can be found in CarEdu source:
Also must be dragged onto the object.
4. In the resume button script there must be some code to activate the hiding script cursor back, see below:
The script must be dragged on the object, too.
In CarEdu, all three scripts were dragged onto PauseMenuCamera object in CompleteScene scene.
Also note that HideCursorScript and UpdateScript are both enabled while PauseMenuScript is disabled by default. It's also very important, as enabling the pause menu script would show the pause menu immediately upon launching the game.
Summing everything up, notice these lines:
Screen.showCursor = false;
This line makes the cursor hidden.
Screen.showCursor = true;
And this one makes it shown.
These, along with enabling/disabling the short HideCursorScript script at the beginning are the core of hiding the cursor.