关闭

UGUI 事件响应之EventSystem

标签: UGUI EventSytem Even
4935人阅读 评论(0) 收藏 举报

在Unity新的UI系统里,按照NGUI写了自己一套事件响应机制。

EventSystem官方描述(http://docs.unity3d.com/460/Documentation/Manual/EventSystem.html)如下:

EventSystem 是一种将基于输入的事件发送到应用程序中的对象,无论是键盘、 鼠标、 触摸或自定义输入。 EventSystem 由发送事件的几个组件共同组成。

Overview

当您将 EventSystem 组件添加到一个游戏对象上,你会发现它并没有多少暴露的功能时,这是因为 EventSystem 本身被设计为一个 manager and facilitator (管理和主持) EventSystem 模块之间的通信。

EventSystem 的主要作用是,如下所示:

• 管理被选中游戏对象

• 管理正在使用的 InputModule

• 管理 Raycasting (如果需要)

• 更新所需的所有 InputModules

Input Modules

输入的模块是 EventSystem 表现的主要逻辑怎样的,它们用于:

• 处理输入

• 管理事件的状态

• 将事件发送到场景物体。

在同一时间只有一个 InputModule 可以在 EventSystem 中处于活动状态,并且他们必须作为 EventSystem 组件的同一游戏物体上的组件。

Raycasters

Raycasters 用于发送确定 pointer 指针位于什么上方,对于 InputModules 使用Raycasters 这是常见的,场景配置中用于计算 pointing 设备是 over 。

有 3 种情况提供的 Raycasters 默认存在:

• GraphicRaycaster- 用于 UI 元素

• 2DPhysicsRaycaster- 用于为 2D 物理元素

• 3DPhysicsRaycaster- 用于为三维物理元素


EventSystem支持的事件包括很多种类型,他们都是以接口的形式存在,包括:

IPointerEnterHandler – OnPointerEnter – Called when a pointer enters the object
IPointerExitHandler – OnPointerExit – Called when a pointer exits the object
IPointerDownHandler – OnPointerDown – Called when a pointer is pressed on the object
IPointerUpHandler – OnPointerUp – Called when a pointer is released (called on the original the pressed object)
IPointerClickHandler – OnPointerClick – Called when a pointer is pressed and released on the same object
IBeginDragHandler – OnBeginDrag – Called on the drag object when dragging is about to begin
IDragHandler – OnDrag – Called on the drag object when a drag is happening
IEndDragHandler – OnEndDrag – Called on the drag object when a drag finishes
IDropHandler – OnDrop – Called on the object where a drag finishes
IScrollHandler – OnScroll – Called when a mouse wheel scrolls
IUpdateSelectedHandler – OnUpdateSelected – Called on the selected object each tick
ISelectHandler – OnSelect – Called when the object becomes the selected object
IDeselectHandler – OnDeselect – Called on the selected object becomes deselected
IMoveHandler – OnMove – Called when a move event occurs (left, right, up, down, ect)
ISubmitHandler – OnSubmit – Called when the submit button is pressed
ICancelHandler – OnCancel – Called when the cancel button is pressed

跟踪下代码可以看到EventTrigger组件是继承了上述各类借口。

在自己的代码里,添加一下类代码:

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;

public class EventTriggerListener : EventTrigger
{
    public delegate void VoidDelegate(GameObject go);
    public VoidDelegate onClick;
    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onUpdateSelect;

    public static EventTriggerListener Get(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null)
            listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null) onClick(gameObject);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown(gameObject);
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onEnter != null) onEnter(gameObject);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null) onExit(gameObject);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null) onUp(gameObject);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect(gameObject);
    }
}

在调用处使用如下:

public class UController : MonoBehaviour {
	Button	button;
	Image image;
	void Start () 
	{
		button = transform.Find("Button").GetComponent<Button>();
		image = transform.Find("Image").GetComponent<Image>();
		EventTriggerListener.Get(button.gameObject).onClick =OnButtonClick;
		EventTriggerListener.Get(image.gameObject).onClick =OnButtonClick;
	}
 
	private void OnButtonClick(GameObject go){
		//在这里监听按钮的点击事件
		if(go == button.gameObject){
			Debug.Log ("Click");
		}
	}
}

这样做的好处:

1.一般GameObject.GetComponet<Button>().onClick.AddListener(_callback);

这里_callback回掉函数是public delegate void Function();的形式,不能传参数,采用上述方式可以添加参数,在做ScrollView列表的时候可以找到具体某个Item;

2.这样做能对项目的Ui事件进行更有效的管理,功能丰富。


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:49145次
    • 积分:1030
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:6篇
    • 译文:0篇
    • 评论:0条
    文章分类