所有的事件接口均继承自 IEventSystemHandler
IPointerEnterHandler 指针进入事件
――利用指针进入、退出事件,可以完成ToolTip功能
IPointerDownHandler 指针按下事件:在可视元素矩形区域内按下时响应
任意地方抬起时响应
IPointerClickHandler 指针点击事件:在可视元素矩形区域内按下后,且在同一可视元素矩形区域内抬起时响应
――有按下,必定有抬起;有按下,未必有点击
IInitializePotentialDragHandler 初始化潜在的拖动事件:与IPointerDownHandler触发条件基本相同
IBeginDragHandler 拖动开始事件
ISelectHandler 选择事件:选择(点击)时调用EventSystem.current.SetSelectedGameObject(gameObject)选中,随后触发OnSelect(...)方法
IUpdateSelectedHandler 选中物体每帧触发事件
――移动、提交、取消操作,必须先选中(OnSelect)才能触发
所有被继承的事件接口,需要实现其方法,参数一般是EventData(BaseEventData/PointerEventData),利用这些参数可以完成相应操作。
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class EventTest : MonoBehaviour,
- IPointerClickHandler,
- IPointerEnterHandler,
- IPointerExitHandler,
- IPointerDownHandler,
- IPointerUpHandler,
- IBeginDragHandler,
- IDragHandler,
- IInitializePotentialDragHandler,
- IEndDragHandler,
- IDropHandler,
- IUpdateSelectedHandler,
- ISelectHandler,
- IDeselectHandler,
- IScrollHandler,
- IMoveHandler,
- ISubmitHandler,
- ICancelHandler
- {
- #region 鼠标指针类
- //鼠标进入时响应
- public void OnPointerEnter(PointerEventData eventData)
- {
- Debug.Log("OnPointerEnter");
- }
- //鼠标离开时响应
- public void OnPointerExit(PointerEventData eventData)
- {
- Debug.Log("OnPointerExit");
- }
- //鼠标按下时响应
- public void OnPointerDown(PointerEventData eventData)
- {
- Debug.Log("OnPointerDown");
- }
- //鼠标释放时响应
- public void OnPointerUp(PointerEventData eventData)
- {
- Debug.Log("OnPointerUp");
- }
- //鼠标点击时响应
- public void OnPointerClick(PointerEventData eventData)
- {
- Debug.Log("OnPointerClick");
- }
- #endregion
- #region 拖拽类
- //初始化拖拽
- public void OnInitializePotentialDrag(PointerEventData eventData)
- {
- Debug.Log("OnInitializePotentialDrag");
- }
- //开始拖拽
- public void OnBeginDrag(PointerEventData eventData)
- {
- Debug.Log("OnBeginDrag");
- }
- //拖拽中
- public void OnDrag(PointerEventData eventData)
- {
- Debug.Log("OnDrag");
- }
- //拖拽结束
- public void OnEndDrag(PointerEventData eventData)
- {
- Debug.Log("OnEndDrag");
- }
- //拖拽释放
- public void OnDrop(PointerEventData eventData)
- {
- Debug.Log("OnDrop");
- }
- #endregion
- #region 点选类
- //当物体被选中时每帧触发
- public void OnUpdateSelected(BaseEventData eventData)
- {
- Debug.Log("OnUpdateSelected");
- }
- //选中物体
- public void OnSelect(BaseEventData eventData)
- {
- Debug.Log("OnSelect");
- }
- //未选中物体
- public void OnDeselect(BaseEventData eventData)
- {
- Debug.Log("OnDeselect");
- }
- #endregion
- #region 输入类
- //鼠标中轮滚动
- public void OnScroll(PointerEventData eventData)
- {
- Debug.Log("OnScroll");
- }
- //移动物体
- public void OnMove(AxisEventData eventData)
- {
- Debug.Log("OnMove");
- }
- //提交
- public void OnSubmit(BaseEventData eventData)
- {
- Debug.Log("OnSubmit");
- }
- //取消
- public void OnCancel(BaseEventData eventData)
- {
- Debug.Log("OnCancel");
- }
- #endregion
- }
文章来源: UGUI事件系统――事件接口