Monday, August 18, 2014

Changing the default Mouse Cursor in Unity (hardware acceleration)

Unity version: 4.5
Language used: C#

I checked some outdated tutorials how to change the default system mouse cursor in Unity but it lagged considerably. So I searched the Unity manual (now why didn't I check there first?). However, I couldn't get this working right away and wondered what was wrong, until I took another look at the inspector. When you're importing your cursor, select the Cursor from the Texture type in the inspector:



Then in your main GameObject (or your camera script) add these lines:


   .
   .
   .

   public Texture2D cursorTexture;
   private Vector2 hotSpot = Vector2.zero;

   void Start() {
      Cursor.SetCursor(cursorTexture, hotSpot, CursorMode.Auto);
   }

   .
   .
   .


CursorMode.Auto means the hardware mouse acceleration will be used on platforms that support it. The hotspot is like the offset location of the cursor. In this case the tip (at 0,0) is the effective part of the cursor.

Next, drag the cursor texture from the Assets to the Cursor Texture field in the object's script:



On Mac, I couldn't get the cursor showing in the editor while testing. Building and running worked fine though.

That's it, thanks for reading. :)

--------------------------------------
Code highlighted with hilite.me

5 comments:

  1. In the Unity editor you can go to Edit -> Project Settings -> Player and set the default cursor texture and hotspot. Doing that doesn't show the cursor in a full screen Mac app when you build and run, though.

    Your technique got that working for me! Thanks!

    ReplyDelete