一、总览表+示例脚本
鼠标事件\ 物体 | UGUI | 3D游戏物体 |
---|---|---|
移入 | OnPointerEnter | OnMouseEnter |
移出 | OnPointerExit | OnMouseExit |
悬置 | 可用OnPointerEnter+OnPointerExit模拟悬置功能 | OnMouSEOver |
点击 | OnPointerClick | 可用OnMouseDown替代此功能 |
按下 | OnPointerDown | OnMouseDown |
抬起 | OnPointerUp | onmouseup |
随便一个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] 举报,一经查实,本站将立刻删除。