鼠标移入UI按钮时改变鼠标指针贴图

1.将指针图片的texture Type设置为Cursor。
2.max size设置为最小的32。
3.将cursor图片拖到检视面板的cursorTexture。
4.代码挂到button上。web

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UICursorSet : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler {
public Texture2D cursorTexture;
public CursorMode cursorMode = CursorMode.Auto;
public Vector2 hotSpot = Vector2.zero;
public void OnPointerEnter(PointerEventData eventData)
{
Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
}
public void OnPointerExit(PointerEventData eventData)
{
Cursor.SetCursor(null, hotSpot, cursorMode);
}
}svg

hint:若是是3D物体的话能够使用OnMouseEnter、OnMouseExit方法。指针

using UnityEngine;
using System.Collections;
public class CursorSet : 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);
}
}xml