【Unity3D / UI】 快速处理鼠标事件的方法

    Unity的UGUI,处理鼠标事件,需要自行实现IPointerClickHandler,IPointerDownHandler等接口,很不方便。

    我们可以把常用接口封装起来,让需要实现鼠标事件的类,继承已经封装好的类即可。


using System;
using UnityEngine;
using UnityEngine.EventSystems;

public class ClickOnListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler {

	#region Action
	public Action ActClick = delegate {};
	public Action ActPress = delegate { };
	public Action ActRelease = delegate { };
	#endregion

	#region Click Event
	/// 
	/// 当鼠标点击时
	/// 
	/// 
	public void OnPointerClick(PointerEventData eventData) {
		ActClick();
	}

	/// 
	/// 当鼠标按下时
	/// 
	/// 
	public void OnPointerDown(PointerEventData eventData) {
		ActPress();
	}

	/// 
	/// 当鼠标抬起时
	/// 
	/// 
	public void OnPointerUp(PointerEventData eventData) {
		ActRelease();
	}

	/// 
	/// 清除
	/// 
	public void Clear() {
		ActClick = null;
		ActPress = null;
		ActRelease = null;
	}

	#endregion
}


    如此一来,只需要让UI类继承  ClickOnListener 类,然后注册 ActClick 等已经封装好的事件即可,比每次都重复实现接口的效率要高不少。

    本例封装了三个接口,有需要可以把UI事件接口全部封装起来(见下文)。不过需要注意一个问题,由于这些接口都是空实现,但是只要写了这个方法,哪怕没有实现,Unity的UI消息机制,也会每次将消息发送到这个方法上,会有一定性能开销。


还可以增加一个静态接口,用于发送鼠标事件:

	#region Static
	/// 
	/// 向指定对象发送点击事件
	/// 
	/// 目标
	public static void SendOnClick(GameObject target) {
		PointerEventData eventData = new PointerEventData(null);
		target.SendMessage("OnPointerClick", eventData);
	} 
	#endregion


最后附上一个比较完整的UI事件监听器类,有需要同时处理较多UI事件的情况可以直接使用。

using UnityEngine;
using UnityEngine.EventSystems;

public class UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerEnterHandler,
	IPointerExitHandler,
	IPointerUpHandler, ISelectHandler, IUpdateSelectedHandler, IDeselectHandler, IDragHandler,
	IEndDragHandler, IDropHandler, IScrollHandler, IMoveHandler
{

	public delegate void UIPointerEventDelegate(GameObject go, PointerEventData data);

	public delegate void UIBaseEventDelegate(GameObject go, BaseEventData data);

	public delegate void UIAxisEventDelegate(GameObject go, AxisEventData data);

	public UIPointerEventDelegate onClick = delegate { };
	public UIPointerEventDelegate onDown = delegate { };
	public UIPointerEventDelegate onEnter = delegate { };
	public UIPointerEventDelegate onExit = delegate { };
	public UIPointerEventDelegate onUp = delegate { };
	public UIBaseEventDelegate onSelect = delegate { };
	public UIBaseEventDelegate onUpdateSelect = delegate { };
	public UIBaseEventDelegate onDeSelect = delegate { };
	public UIPointerEventDelegate onDrag = delegate { };
	public UIPointerEventDelegate onEndDrag = delegate { };
	public UIPointerEventDelegate onDrop = delegate { };
	public UIPointerEventDelegate onScroll = delegate { };
	public UIAxisEventDelegate onMove = delegate { };

	/// 
	/// 用于对任意UI对象快速获取UI事件监听器
	/// 
	/// 对象
	/// 结果
	public static UIEventListener Get(GameObject go)
	{
		return go.GetOrAddComponent();
	}

	public void OnPointerClick(PointerEventData eventData)
	{
		onClick(gameObject, eventData);
	}

	public void OnPointerDown(PointerEventData eventData)
	{
		onDown(gameObject, eventData);
	}

	public void OnPointerEnter(PointerEventData eventData)
	{
		onEnter(gameObject, eventData);
	}

	public void OnPointerExit(PointerEventData eventData)
	{
		onExit(gameObject, eventData);
	}

	public void OnPointerUp(PointerEventData eventData)
	{
		onUp(gameObject, eventData);
	}

	public void OnSelect(BaseEventData eventData)
	{
		onSelect(gameObject, eventData);
	}

	public void OnUpdateSelected(BaseEventData eventData)
	{
		onUpdateSelect(gameObject, eventData);
	}

	public void OnDeselect(BaseEventData eventData)
	{
		onDeSelect(gameObject, eventData);
	}

	public void OnDrag(PointerEventData eventData)
	{
		onDrag(gameObject, eventData);
	}

	public void OnEndDrag(PointerEventData eventData)
	{
		onEndDrag(gameObject, eventData);
	}

	public void OnDrop(PointerEventData eventData)
	{
		onDrop(gameObject, eventData);
	}

	public void OnScroll(PointerEventData eventData)
	{
		onScroll(gameObject, eventData);
	}

	public void OnMove(AxisEventData eventData)
	{
		onMove(gameObject, eventData);
	}
}


你可能感兴趣的:(Unity3D)