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

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);
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值