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 recently made, Puzzledorf, uses 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 in Puzzledorf looks like below:

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 and I haven’t yet found a way around it, but it will look fine in the build if it’s full screen. If you use Windowed mode or a smaller resolution, it will look less nice.

Note: I actually took mouse control out of the menus in the final product of Puzzledorf but the above is what I did during development.

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.

If your cursor disappears between scenes, it’s probably because the game object your Custom Mouse Cursor script is attached was destroyed when you went between scenes.

Leave a comment

Blog at WordPress.com.

Up ↑