UGUI部分事件接口讲解以及一个很简单动态添加事件类

2:切入正题

在ugui中利用事件接口实现事件,比如实现IPointerDownHandler,IPointerClickHandler等等这些常用的事件接口,还有一些接口在这说明一下,因为不知道的 还真不知道怎么使用,(先上图)

UGUI部分事件接口讲解以及一个很简单动态添加事件类_第1张图片

另外的IDropHandler接口实现OnDrop(我理解为 接收函数 关于拖动这块建议去Asset store上面看看官方(Unity Samples:UI),里面有拖拽这个例子)

UGUI部分事件接口讲解以及一个很简单动态添加事件类_第2张图片

UGUI部分事件接口讲解以及一个很简单动态添加事件类_第3张图片

好接下来重点来了,一个很简单的如何实现动态添加事件(在网上看的动态添加事件都很麻烦的,各种注册回调,反正用起来不是很方便,如果你使用过NGUI应该知道UITriggerListener来动态添加事件很方便)这里可以先看看雨松的帖子(致敬雨松)帖子点这里。然后我们再用的时候大部分是希望要知道PointerEventData里面的数据,所以在这里我改了一下(先上图)

UGUI部分事件接口讲解以及一个很简单动态添加事件类_第4张图片

然后我们再用的时候

UGUI部分事件接口讲解以及一个很简单动态添加事件类_第5张图片

这样是不是很方便呢??????

最后,按照国际惯例,附上源码

 

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class UIEvent : MonoBehaviour,
    ISelectHandler,ISubmitHandler,ICancelHandler,IMoveHandler,IUpdateSelectedHandler,   //要设置选中的物体
    IDropHandler,IDragHandler,IPointerDownHandler {
        //鼠标按下
        public void OnPointerDown(PointerEventData eventData)
        {
            //只有设置了下面的事件才会响应
           EventSystem.current.SetSelectedGameObject(this.gameObject);
        }
        //选中这个物体(既是鼠标焦点在这个物体上)
        public void OnSelect(BaseEventData eventData)
        {
            Debug.Log("选中");
        }
        public void OnUpdateSelected(BaseEventData eventData)
        {
            Debug.Log("Update选中");
        }
        //在inputManagerd对用Submit
        public void OnSubmit(BaseEventData eventData)
        {
            Debug.Log("提交");
        }
        //在inputManagerd对用Cancel
        public void OnCancel(BaseEventData eventData)
        {
            Debug.Log("取消");
        }
        //在inputManagerd对用Horizontal和Vertical
        public void OnMove(AxisEventData eventData)
        {
            Debug.Log("移动"+eventData.moveDir);
        } 
  

    //拖动接收事件
    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("接收的物体名字:——"+eventData.pointerEnter.gameObject.name);
    }
    //拖动事件
    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("拖动中");
    }

另一个

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class EventTriggerListener :EventTrigger
{
    public delegate void VoidDelegate(T eventData);
    public VoidDelegate onClick;
    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onUpdateSelect;
    public VoidDelegate onMove;

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

//-------------------------2019.4.19 修改增加--------------

把EventTriggerListener改一下

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class EventTriggerListener : EventTrigger
{
    public EventTriggerConstom OnMouseClick = new EventTriggerConstom();
    public EventTriggerConstom OnMouseDown=new EventTriggerConstom();
    public EventTriggerConstom OnMouseEnter=new EventTriggerConstom();
    public EventTriggerConstom OnMouseExit=new EventTriggerConstom();
    public EventTriggerConstom OnMouseUp=new EventTriggerConstom();
    public EventTriggerConstom OnMouseSelect=new EventTriggerConstom();
    public EventTriggerConstom OnMouseUpdateSelect=new EventTriggerConstom();
    public EventTriggerConstom OnMouseMove=new EventTriggerConstom();

    public static EventTriggerListener Get(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent() ?? go.AddComponent();
        return listener;
    }
    public override void OnPointerClick(PointerEventData eventData)
    {

        if (OnMouseClick != null) OnMouseClick.Invoke(eventData);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (OnMouseDown != null) OnMouseDown.Invoke(eventData);
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (OnMouseEnter != null) OnMouseEnter.Invoke(eventData);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (OnMouseExit != null) OnMouseExit.Invoke(eventData);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (OnMouseUp != null) OnMouseUp.Invoke(eventData);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (OnMouseSelect != null) OnMouseSelect.Invoke(eventData);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (OnMouseUpdateSelect != null) OnMouseUpdateSelect.Invoke(eventData);
    }
    public override void OnMove(AxisEventData eventData)
    {
        if (OnMouseMove != null) OnMouseMove.Invoke(eventData);
    }

    //-------------
    public class EventTriggerConstom : UnityEvent
    { }
}

 

你可能感兴趣的:(unity)