Unity动态创建UI点击事件(不需要添加UIButton组件)

Unity中UGUI中绑定UI的按钮事件有4中方式,具体参考该文章:https://www.cnblogs.com/isayes/p/6370168.html

而往往项目中UI的按钮事件比较多,例如拖拽,鼠标按下,鼠标抬起等等,一一创建并手动添加组件太麻烦,不够灵活。于是我自己写了个工具,在UI只有Image组件的情况下代码动态创建各类鼠标以及触摸点击事件的各类监听。具体用法如下图:

Unity动态创建UI点击事件(不需要添加UIButton组件)_第1张图片

其中Page[i]代表GameObject类型的游戏物体,OnDrag的参数go代表当前拖拽的游戏物体。当然不仅是拖拽,还有其他点击事件等等。该工具代码如下:

/*
 * 让UGUI监听的方式和NGUI的方式一样
 * 
 * 可以依次类推进行扩展
 * 
 * 引用:http://www.xuanyusong.com/archives/3325
 */

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class EventTriggerListener : 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 onMove;
    public VoidDelegate onDrop;
    public VoidDelegate onDeselect;
    public VoidDelegate onScroll;
    public VoidDelegate onBeginDrag;
    public VoidDelegate onCancel;
    public VoidDelegate onEndDrag;
    public VoidDelegate onInitializePotentialDrag;
    public VoidDelegate onSubmit;

    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(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 override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(gameObject);
    }

    public override void OnDrop(PointerEventData eventData)
    {
        if (onDrop != null) onDrop(gameObject);
    }

    public override void OnMove(AxisEventData eventData)
    {
        if (onMove != null) onMove(gameObject);
    }

    public override void OnDeselect(BaseEventData eventData)
    {
        if (onDeselect != null) onDeselect(gameObject);
    }

    public override void OnScroll(PointerEventData eventData)
    {
        if (onScroll != null) onScroll(gameObject);
    }

    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (onBeginDrag != null) onBeginDrag(gameObject);
    }

    public override void OnEndDrag(PointerEventData eventData)
    {
        if (onEndDrag != null) onEndDrag(gameObject);
    }

    public override void OnInitializePotentialDrag(PointerEventData eventData)
    {
        if (onInitializePotentialDrag != null) onInitializePotentialDrag(gameObject);
    }

    public override void OnSubmit(BaseEventData eventData)
    {
        if (onSubmit != null) onSubmit(gameObject);
    }

    public override void OnCancel(BaseEventData eventData)
    {
        if (onCancel != null) onCancel(gameObject);
    }
}

 

你可能感兴趣的:(Unity动态创建UI点击事件(不需要添加UIButton组件))