This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

  • 📢 Blog home: juejin.cn/user/139024…
  • 📢 welcome to like 👍 collect ⭐ message 📝 if there is an error please correct!
  • 📢 This article was originally written by dumb code Y at 🙉
  • 📢 The future is long and worth striving for a better life ✨

🎬 learn how to change mouse styles in Unity projects!

📢 preface

  • We often see in-game mice with their own image style
  • In this way, when playing the game, I will be more involved in the game
  • So this article is about how to change the style of mouse in Unity

🏳️🌈 Method 1: Modify the mouse style in the editor

  • It’s super easy, and once you’re done, the mouse will change to the style we changed in Unity’s Game view
  • Whether the program is started or not, the mouse style will change to the style we set

Take a look inside!

Modify image styles

  • First we need an image instead of the default mouse style
  • Look for one on the Internet and give it a try!

  • For example, if I take a few images above and import them into Unity, the images will be in Default format by Default
  • This format is not acceptable, we need to process, change these pictures to Cursor format
  • To modify: Select the image and change Texture Type to Cursor in the Properties panel as shown below
  • Remember to click Apply in the lower right corner after the change!

Modify the Mouse style

Click Edit->Project Setting->Player->Default Cursor and drag and assign values

As shown below:

  • Just change this one, then go back to the Game view and move the mouse over it to see that the mouse style has changed to the image we set!
  • As shown below, this will only change in the Game view, the rest of the Game will remain the same!

🏳️🌈 Second method: Modify the file using code

  • The first modification method is indeed simple, but lacks practical effect
  • Since it can only maintain one style and can’t change it under certain circumstances, we need to change the style in the code

Core code definition

The core code is also very simple, with the following line:

 Cursor.SetCursor(cursorTexture1, Vector2.zero, CursorMode.Auto);
Copy the code

The parameters in the method are:

  1. Texture2D texture, // The cursor image to replace
  2. Vector2 hotspot, // Response area (Vector2.zero)
  3. CursorMode CursorMode // Render mode. Auto is platform adaptive display

Just call this line of code when you want to change the mouse style and assign the first parameter to the image style you want!

Example code operation

Now that I know how to modify it, I’ll go straight to the full code and see what happens

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Test1 : MonoBehaviour.IPointerEnterHandler.IPointerExitHandler.IPointerUpHandler.IPointerDownHandler
{
    public Button _btn1;

    public Texture2D cursorTexture1;// Replace cursor picture 1
    public Texture2D cursorTexture2;// Cursor image 2 to replace
    public Texture2D cursorTexture3;// Replace cursor picture 3

    Texture2D t1;

    private void Awake()
    {
        _btn1.onClick.AddListener(() => { Cursor.SetCursor(cursorTexture3, Vector2.zero, CursorMode.Auto); });
    }
    
    // Execute after mouse enters 3D object
    void OnMouseEnter()
    {
        Cursor.SetCursor(cursorTexture1, Vector2.zero, CursorMode.Auto);
    }
    // Execute after the mouse moves away from the 3D object
    void OnMouseExit()
    {
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
    }
    // Execute after mouse enters UI
    public void OnPointerEnter(PointerEventData eventData)
    {
        Cursor.SetCursor(cursorTexture1, Vector2.zero, CursorMode.Auto);
    }
    // Execute after mouse mouse leaves UI
    public void OnPointerExit(PointerEventData eventData)
    {
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
    }
    // Execute after mouse click in UI
    public void OnPointerDown(PointerEventData eventData)
    {
        Cursor.SetCursor(cursorTexture2, Vector2.zero, CursorMode.Auto);
    }
    // Execute after mouse lift in UI
    public void OnPointerUp(PointerEventData eventData){ Cursor.SetCursor(cursorTexture1, Vector2.zero, CursorMode.Auto); }}Copy the code
  • Mount the script to the UI you want to change and a 3D object, then drag the image onto it
  • Note that the image style is still Cursor!

  • I have added several mouse interfaces to the code, namely the callback method after the mouse enters the UI, leaves the UI, clicks and lifts!
  • Then make a different mouse style switch in different callbacks. Here’s how it works in practice:

  • You can see that the default is white, which was set in the first method.
  • It turns yellow when you enter the following UI, and turns green when you press the mouse in that UI
  • The 3D Cube also turns yellow when you enter it, and turns blue when you click the test button
  • This is set up in the code, can be modified according to their own needs!

👥 summary

  • This article describes how to modify mouse styles in Unity in two ways
  • As well as write a simple example to see the effect, in the actual project development or will use!
  • Have you learned today’s tip yet? If it is useful, remember to support it