微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity 鼠标事件总结UGUI + 游戏物体

一、总览表+示例脚本

鼠标事件\ 物体UGUI3D游戏物体
移入OnPointerEnterOnMouseEnter
移出OnPointerExitOnMouseExit
悬置可用OnPointerEnter+OnPointerExit模拟悬置功能OnMouSEOver
点击OnPointerClick可用OnMouseDown替代此功能
按下OnPointerDownOnMouseDown
抬起OnPointerUponmouseup

随便一个UGUI组件(注意引用UnityEngine.EventSystems命名空间),一个3D物体(带碰撞体),分别在它们身上加以下两个脚本:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class TestUnityMouseEvent_UGUI : MonoBehavIoUr,IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{
    public void OnPointerEnter(PointerEventData eventData)    
    {
        Debug.Log("UGUI部件上,鼠标【移入】");
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("UGUI部件上,鼠标【移出】");
    }


    //没有“悬置”,可用OnPointerEnter+OnPointerExit模拟,例如:鼠标放在角色卡片,显示角色的生命值、攻击力、防御力


    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("UGUI部件上,鼠标【点击】");
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("UGUI部件上,鼠标【按下】");
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("UGUI部件上,鼠标【抬起】");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestUnityMouseEvent_3dGameObject : MonoBehavIoUr
{

    public void onm ouseEnter()
    {
        Debug.Log("立方体上,鼠标【移入】");
    }
    public void onm ouseExit()
    {
        Debug.Log("立方体上,鼠标【移出】");
    }
    public void onm ouSEOver()
    {
        Debug.Log("立方体上,鼠标【悬置】");
    }


    //没有“点击”,一般用OnMouseDown来模拟


    public void onm ouseDown()
    {
        Debug.Log("立方体上,鼠标【按下】");
    }
    public void onm ouseUp()
    {
        Debug.Log("立方体上,鼠标【抬起】");
    }
}

二、官方API

1、UGUI方法:它们是UnityEngine事件系统,功能接口的实现方法

在这里插入图片描述

2、3D 游戏物体方法:它们是MonobehavIoUr类的“消息”

在这里插入图片描述

在这里插入图片描述

三、补充(略,没时间写了)

1、UGUI:用OnPointerEnter+OnPointerExit模拟悬置功能
2、3D游戏物体:用OnMouseDown实现点击功能、双击功能

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐