【UGUI】 UI点击事件处理

记录一个UIEventListener类,在撸UI代码时,很多时候用到UI的点击事件,不紧紧时Button的,其他Image,RawImage有时也需要用到,而且有时候要把UI本身传进去用,原生的Onclick.AddListener(),默认是无参的回调函数,下面码一个所有UI都可用的点击事件类。

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

public class UIEventListener : UnityEngine.EventSystems.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 VoidDelegate onDrag;
    public VoidDelegate onEndDrag;
    public VoidDelegate onBeginDrag;

    public delegate void ToggleDelegate(Toggle toggle);
    public ToggleDelegate onToggleChange;

    public delegate void EventDelegate(PointerEventData eventData);
    public EventDelegate onPointerDown;
    public EventDelegate onPointerUp;
    public EventDelegate onDraging;
    static public UIEventListener Get(UIBehaviour ui)
    {
        GameObject go = ui.gameObject;
        UIEventListener listener = go.GetComponent<UIEventListener>();
        if (listener == null) listener = go.AddComponent<UIEventListener>();
        return listener;
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null) onClick(gameObject);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown(gameObject);
        if (onPointerDown != null) onPointerDown(eventData);
    }
    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);
        if (onPointerUp != null) onPointerUp(eventData);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject);
        if (onToggleChange != null) onToggleChange(gameObject.GetComponent<Toggle>());
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect(gameObject);
        if (onToggleChange != null) onToggleChange(gameObject.GetComponent<Toggle>());
    }
    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(gameObject);
        if (onDraging != null) onDraging(eventData);
    }
    public override void OnEndDrag(PointerEventData eventData)
    {
        if (onEndDrag != null) onEndDrag(gameObject);
    }
    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (onBeginDrag != null) onBeginDrag(gameObject);
    }
}

用法很简单,类似Button的代码回调

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour {

    Image img;

	void Start () {
        UIEventListener.Get(img).onClick = OnClick;
    }
	
    void OnClick(GameObject go)
    {
        Debug.Log(go.name + "被点击了....");
    }	
}

所有UI都适用,参数为该UI本身。

上一篇:ugui拖拽


下一篇:gradient 线性渐变 浏览器兼容