How To Make A Custom Mouse Cursor – Unity

This is a quick guide on how to make a 2D, custom mouse cursor in Unity, from making the texture, getting it into Unity, and then actually using it in your game.

The Texture

The first thing you need is a custom mouse texture. I recommend saving it as a PNG, or otherwise JPEG. According to some quick research, the Windows cursor seems to typically be either 32×32 pixels or 48×48 pixels.

The game I am making is a pixel art game, using 12×12 pixel sprites, so I made the custom mouse cursor at the same size, however, I increase all of the graphics by 4x when I export, making the cursor 48×48 pixels on screen. The end result looks like below:

(Above is pictured a work in progress screenshot from my upcoming puzzle game, Puzzledorf)

You can see from the above screenshot how a 48×48 pixel mouse cursor looks in a game at HD resolution. Try experimenting with a few different sizes if you’re not sure, like: 32×32, 48×48, and maybe 24×24.

Bear in mind, though, that the cursor can look quite different when testing inside Unity. The above screenshot is from an actual build in full HD. Below is what the 48×48 cursor looks like inside Unity:

Don’t ask me why Unity does something so bizarre, it simply does, but it will look fine in the build.

Importing The Texture For Use

Once you have your final cursor image, just put it inside your Assets folder in Unity, then click on the image and set it’s import settings as follows:

The most important part is having the Cursor texture type.

Using Clamp I believe helps the image to remain crisp and not blur or get weird visual glitches. Using Point (no filter) also helps achieve crisp 2D images, especially for pixel art.

I am not entirely sure why but having the Format as RGBA 32 bit is the way that Unity seems to prefer it.

The Code

To get your cursor in is really simple. Make a new CustomMouseCursor script as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CustomMouseCursor : MonoBehaviour
{
    public Texture2D mouseCursor;

    Vector2 hotSpot = new Vector2(0,0);
    CursorMode cursorMode = CursorMode.Auto;

    private void Start()
    {        
        Cursor.SetCursor(mouseCursor, hotSpot, cursorMode);
    }
}

That’s it. Now just save that script, attach it to a game object in a scene, and drag your texture for the cursor onto the public variable mouseCursor in the inspector.

Below is an explanation of the code:

  • public Texture2D mouseCursor is your mouse cursor texture
  • Vector2 hotSpot is the point where clicks are registered on your mouse cursor, 0,0 is the top left corner of the texture
  • CursorMode cursorMode defines how you want the texture rendered
    private void Start()
    {        
        Cursor.SetCursor(mouseCursor, hotSpot, cursorMode);
    }

Start() runs when the game object in the scene becomes active.

Cursor.SetCursor() is a function that allows us to set the mouse cursor. We give it:

  • the texture of the mouse cursor.
  • where the hot spot of the cursor should be. It’s where your clicks are registered. 0,0 means you will click from the top left of the texture.

    If you are using a cross-hair, and want the hot spot to be the center of the cursor, then set the position to halfway, ie, if your texture is 48×48 pixels, use Vector2 (24, 24) and then the clickable space should be the center of your cursor.
  • The cursor mode, which gives you two modes, Auto and ForceSoftware. Auto means it will try and use your custom mouse cursor, but if it can’t, it will switch back to the systems default mouse cursor. ForceSoftware will always try and force your custom mouse cursor, but it leaves you with no back up if something goes wrong.

Stop The Cursor Disappearing

Now, if your game project uses multiple scenes, you might notice your mouse cursor disappear. If for some reason that happens, it could be that the game object your Custom Mouse Cursor script is attached was destroyed when you went between scenes. To stop a game object being destroyed between scenes, read my tutorial on that here.

Conclusion

Hopefully that helps you create a cool custom cursor for your game project. If for any reason you have a problem, feel free to leave a comment and I will try and get back to you (comments do not automatically appear, I moderate them to stop spam).

If you enjoyed this article, try my game Puzzledorf out on Steam and Humble Bundle.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: