Pages

Monday, January 14, 2013

Unity3d - Screen.lockCursor, Screen.showCursor

Sometimes you just don't want to have a mouse cursor. Sometimes you do.

Thankfully Unity gives you the chance to have control over this with these two very handy items.

Screen.lockCursor

Screen.showCursor 

So very simply you can hide the cursor to have a custom cursor, or lock the cursor so that it doesn't get in the way. A lot of different uses.

For a mouse controlled game I was testing out the features with the following code in void Start().


Screen.lockCursor = true;

Screen.showCursor = false;


*tip* Make sure you reverse this affect if there is a need to use the mouse at some point in the game(ex. menus). 

I was also testing out hiding the cursor and locking it during certain events. In this case I wanted to move the character in a 3d environment with the right mouse button(button 1). This snippet worked when placed in void OnGUI().

 if (Event.current.button == 1 && Event.current.isMouse)

   {   

       Screen.lockCursor = true;

       Screen.showCursor = false;


       if (Event.current.type == EventType.MouseUp)
       {
            Screen.lockCursor = false;

            Screen.showCursor = true;
       }
  
   } 
This covers two simple uses that I have implemented into a game. Not a whole lot of trickery going on here, but thought I would share how I used it.

No comments:

Post a Comment