Fork me on GitHub

Custom Mouse Cursor Tutorial

03 November 2020

So today we are going to be making custom mouse cursor in unity.

Currently I am using Unity 2019.4.10, So this can be applicable for future versions and maybe some older ones too.

We shall start with the default Sample Scene

I use VS-Code IDE as it is light weight, but you can use any IDE of your choice.
Download the assets here to get started along with me.





Lets start by creating a C# script and call it GameManager.cs
so this is the default script you will get

    using UnityEngine;

    public class GameManager : MonoBehaviour{
        
        //Use..
        void Start(){
            
        }

        //....
        void Update(){

        }
    }



You can remove both the comments.
First we are going to declare 2 texture2ds before our start function and call them hand and pointer.

    public Texture2D pointer;
    public Texture2D hand;



You can delete the Update function but leave the Start function as it is.
Now we will create 2 functions and call it setPointer and setHand.

    public void setHand(){
        
    }

    public void setPointer(){
        
    }



You can use Cursor.SetCursor() function to set the cursor through script.
It requires 3 parameters texture vector2 hotspot and cursormode.
We`ll set the hotspot to 0,0 and cursormode to force-software.

    public void setHand(){
        Cursor.SetCursor(hand, new Vector2(0, 0), CursorMode.ForceSoftware);
    }

    public void setPointer(){
        Cursor.SetCursor(pointer, new Vector2(0, 0), CursorMode.ForceSoftware);
    }



And so this is the final script.

    using UnityEngine;

    public class GameManager : MonoBehaviour
    {
        public Texture2D pointer;
        public Texture2D hand;

        void Start()
        {
            setPointer();
            //setting the cursor to pointer on start
        }

        public void setHand(){
            Cursor.SetCursor(hand, new Vector2(0, 0), CursorMode.ForceSoftware);
        }

        public void setPointer(){
            Cursor.SetCursor(pointer, new Vector2(0, 0), CursorMode.ForceSoftware);
        }
    }



Now go into the unity editor and hit play.
Everything should work as expected.