
Unity Cookbook
By :

Cursor icons are often used to indicate the nature of the interactions that can be done with the mouse. Zooming, for instance, might be illustrated by a magnifying glass; shooting, on the other hand, is usually represented by a stylized target or reticle:
Figure 2.23: Mouse pointer represented as a stylized target
The preceding screenshot shows an example of the Unity logo with the cursor represented as a stylized target. In this recipe, we will learn how to implement custom mouse cursor icons to better illustrate your gameplay – or just to escape the Windows, macOS, and Linux default UI.
For this recipe, we have prepared the folders that you’ll need in the 02_07
folder.
To make a custom cursor appear when the mouse is over a GameObject, follow these steps:
Images
. Select the unity_logo image in the Project window. Then, in the Inspector window, change Texture Type to Sprite (2D and UI). This is because we’ll use this image for a 2D Sprite
GameObject and it requires this Texture Type (it won’t work with the Default type).New Sprite
, if this wasn’t the default name when it was created:(3,3,3)
and, if necessary, reposition Sprite so that it’s centered in the Game window when the scene runs.Sprite
GameObject. This is needed for this GameObject to receive OnMouseEnter
and OnMouseExit
event messages.IconsCursors
. Select all three images in the Project window and, in the Inspector window, change Texture Type to Cursor. This will allow us to use these images as mouse cursors without any errors occurring.CustomCursorPointer
containing the following code and add an instance as a scripted component to the New Sprite
GameObject:
using UnityEngine;
public class CustomCursorPointer : MonoBehaviour {
public Texture2D cursorTexture2D;
private CursorMode cursorMode = CursorMode.Auto;
private Vector2 hotSpot = Vector2.zero;
public void OnMouseEnter() {
SetCustomCursor(cursorTexture2D);
}
public void OnMouseExit() {
SetCustomCursor(null);
}
private void SetCustomCursor(Texture2D curText){
Cursor.SetCursor(curText, hotSpot, cursorMode);
}
}
The OnMouseEnter()
and OnMouseExit()
event methods have been deliberately declared as public
. This will allow these methods to also be called from UI GameObjects when they receive the OnPointerEnterExit
events.
CursorTarget
image into the public Cursor Texture 2D variable slot in the Inspector window for the Custom Cursor Pointer (Script) component:Figure 2.24: Cursor Texture 2D dragged to the variable slot
In this recipe, you created a Sprite
GameObject and assigned it the Unity logo image. You imported some cursor images and set their Texture Type to Cursor so that they can be used to change the image for the user’s mouse pointer. You also added a Box Collider to the Sprite
GameObject so that it would receive OnMouseEnter and OnMouseExit event messages.
Then, you created the CustomCursorPointer
script
class and added an instance object of this class to the Sprite
GameObject. This script tells Unity to change the mouse pointer when an OnMouseEnter message is received – that is, when the user’s mouse pointer moves over the part of the screen where the Unity logo’s sprite image is being rendered. When an OnMouseExit event is received (the user’s mouse pointer is no longer over the cube part of the screen), the system is told to go back to the operating system’s default cursor. This event should be received within a few milliseconds of the user’s mouse exiting from the collider.
Finally, you selected the CursorTarget
image to be the custom mouse cursor image the user sees when the mouse is over the Unity logo image.