一、总览表+示例脚本
鼠标事件\ 物体
|
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("立方体上,鼠标【抬起】");
}
}