unity set cursor code example

Example 1: how to change player cursor c# script unity

using UnityEngine;
using System.Collections;public class ExampleClass : MonoBehaviour
{
    public Texture2D cursorTexture;
    public CursorMode cursorMode = CursorMode.Auto;
    public Vector2 hotSpot = Vector2.zero;
    void OnMouseEnter()
    {
        Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
    }    void OnMouseExit()
    {
        Cursor.SetCursor(null, Vector2.zero, cursorMode);
    }
}

Example 2: unity set cursor position

//C#
    using System.Runtime.InteropServices;
    [DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);
    int xPos = 30, yPos = 1000;   
    SetCursorPos(xPos,yPos);//Call this when you want to set the mouse position

Tags:

Misc Example