Useful code for developers how to recolor Cursor in unity

This is just custom color of cursor in unity. I hope this works for them nicely, I’ve wrote this by myself.

using TMPro;
using UnityEngine;

public class CustomMouseCursor : MonoBehaviour
{
    [Header("Cursor Settings")]
    [SerializeField] private Texture2D cursorTexture; // Your custom cursor image
    [SerializeField] private Color cursorColor = Color.white; // Default color (white preserves original colors)
    [SerializeField] private Vector2 hotSpot = Vector2.zero; // The "active point" of the cursor
    [SerializeField] private Color targetColor; //Desrired plauer color 
    [SerializeField, Range(0.1f, 5f)] private float cursorScale = 1f; // Scale of the cursor
    [SerializeField] private bool smoothMovement = true;
    [SerializeField] private float followSmoothness = 5f;
    SpriteRenderer cursorRenderer;
    private Vector3 targetPosition;
    void Awake()
    {
        // Create a new GameObject for the cursor
        GameObject cursorObject = new GameObject("CustomCursor");
        cursorRenderer = cursorObject.AddComponent<SpriteRenderer>();
        // Convert texture to sprite
        if (cursorTexture != null)
        {
            Sprite cursorSprite = Sprite.Create(
                cursorTexture,
                new Rect(0, 0, cursorTexture.width, cursorTexture.height),
                new Vector2(0.5f, 0.5f), // Center pivot
                100 // Pixels per unit
            );

            cursorRenderer.sprite = cursorSprite;
            cursorRenderer.color = cursorColor;
            cursorRenderer.sortingOrder = 999; // Make sure it renders on top
        }

        // Hide the hardware cursor
        Cursor.visible = false;
    }
    void Update()
    {
        if (cursorRenderer == null) return;

        // Update cursor position
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mousePosition.z = 0;

        if (smoothMovement)
        {
            targetPosition = mousePosition;
            transform.position = Vector3.Lerp(transform.position, targetPosition, followSmoothness * Time.deltaTime);
        }
        else
        {
            transform.position = mousePosition;
        }

        // Handle scaling
        transform.localScale = Vector3.one * cursorScale;
    }
    public void SetCursor(Color color)
    {
        if (cursorTexture == null) return;

        // Create a temporary texture to avoid modifying the original
        Texture2D coloredCursor = new Texture2D(cursorTexture.width, cursorTexture.height, TextureFormat.ARGB32, false);
        coloredCursor.filterMode = FilterMode.Point; // Preserve pixel art quality

        // Apply color to each pixel while preserving alpha
        Color[] pixels = cursorTexture.GetPixels();
        for (int i = 0; i < pixels.Length; i++)
        {
            pixels[i] = new Color(
                pixels[i].r * color.r,
                pixels[i].g * color.g,
                pixels[i].b * color.b,
                pixels[i].a // Preserve original alpha
            );
        }

        coloredCursor.SetPixels(pixels);
        coloredCursor.Apply();

        // Set the cursor
        Cursor.SetCursor(coloredCursor, hotSpot, CursorMode.Auto);

        // Update current color
        cursorColor = color;
    }

    void OnDestroy()
    {
        // Reset to default cursor when this object is destroyed
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
    }

    // Optional: Public method to change color from other scripts
    public void ChangeCursorColor(Color newColor)
    {
        targetColor = newColor;
    }
}

I hope this EHG finds useful.

1 Like