1.将指针图片的texture Type设置为Cursor。
2.max size设置为最小的32。
3.将cursor图片拖到检视面板的cursorTexture。
4.代码挂到button上。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
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);
}
}
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);
}
}
使用系统默认的 .cur文件;注意放到streamingAssetsPath 下。并重命名为normal.cur click.cur;enter exit方法看着绑一下;
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class ChangeCursor : MonoBehaviour
{
/*光标资源加载函数
* fileName为加载路径下的.cur文件
*/
[DllImport("User32.DLL")]
public static extern IntPtr LoadCursorFromFile(string fileName);
/*设置系统指针函数(用hcur替换id定义的光标)
* hcur用于表示指针或句柄的特定类型,可以用LoadCursorFromFile函数加载一个路径下的.cur指针文件
* ID是系统光标标识符,例:
* OCR_APPSTARTING:标准箭头和小的沙漏;32650
* OCR_NORAAC:标准箭头;32512
* OCR_CROSS:交叉十字线光标;32515
* OCR_HAND:手的形状(WindowsNT5.0和以后版本);32649
* OCR_HELP:箭头和向东标记;32651
* OCR_IBEAM:I形梁;32513
* OCR_NO:斜的圆;32648
* OCR_SIZEALL:四个方位的箭头分别指向北、南、东、西;32646
* OCR_SIZENESEW:双箭头分别指向东北和西南;32643
* OCR_SIZENS:双箭头,分别指向北和南;32645
* OCR_SIZENWSE:双箭头分别指向西北和东南;32642
* OCR_SIZEWE:双箭头分别指向西和东;32644
* OCR_UP:垂直箭头: 32516
* OCR_WAIT:32514 沙漏返回值:如果成功,返回非零值;如果失败,返回值为零。
*/
[DllImport("User32.DLL")]
public static extern bool SetSystemCursor(IntPtr hcur, uint id);
private const uint OCR_NORMAL = 32512;
private const uint OCR_HAND = 32649;
private const uint OCR_IBEAM = 32513;
/*查询或设置的系统级参数函数
* uiAction该参数指定要查询或设置的系统级参数,SPI_SETCURSORS:重置系统光标
* fWinIni该参数指定在更新用户配置文件之后广播SPI_SENDWININICHANGE消息
*/
[DllImport("User32.DLL")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
public const uint SPI_SETCURSORS = 87;
public const uint SPIF_SENDWININICHANGE = 2;
/*设置系统自带图标
* SetSystemCursor(Cursors.WaitCursor.CopyHandle(), OCR_NORMAL);
*/
/*设置外部图标文件
* SetSystemCursor(hcur_normal, OCR_NORMAL);
*/
private string path;
void Start()
{
path = UnityEngine.Application.streamingAssetsPath;
}
public void Enter()
{
IntPtr hcur_click = LoadCursorFromFile(path + "/click.cur");
SetSystemCursor(hcur_click, OCR_NORMAL);
}
public void Exit()
{
IntPtr hcur_normal = LoadCursorFromFile(path + "/normal.cur");
SetSystemCursor(hcur_normal, OCR_NORMAL);
}
void OnApplicationQuit()
{
SystemParametersInfo(SPI_SETCURSORS, 0, IntPtr.Zero, SPIF_SENDWININICHANGE);
}
}