Unity通用UI事件监听器

这篇文章展示了如何在Unity3D中创建一个名为WXListener的脚本,用于处理UI元素的各种点击和拖动事件。通过WXUtils.UI库提供的静态方法,可以方便地为GameObject和Transform添加事件监听器,并传递自定义参数。Test类展示了如何使用这些事件处理函数。
摘要由CSDN通过智能技术生成
using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace WXUtils.UI
{
    public class WXListener : MonoBehaviour,
        IPointerClickHandler,
        IPointerDownHandler,
        IPointerUpHandler,
        IBeginDragHandler,
        IDragHandler,
        IEndDragHandler
    {
        public Action<PointerEventData , object []> onClick;
        public Action<PointerEventData , object []> onClickDown;
        public Action<PointerEventData , object []> onClickUp;
        public Action<PointerEventData , object []> onBeginDrag;
        public Action<PointerEventData , object []> onDrag;
        public Action<PointerEventData , object []> onEndDrag;
        public object [] args = null;

        public void OnPointerClick(PointerEventData eventData) {
            onClick?.Invoke(eventData , args);
        }
        public void OnBeginDrag(PointerEventData eventData) {
            onBeginDrag?.Invoke(eventData , args);
        }
        public void OnDrag(PointerEventData eventData) {
            onDrag?.Invoke(eventData , args);
        }
        public void OnEndDrag(PointerEventData eventData) {
            onEndDrag?.Invoke(eventData , args);
        }
        public void OnPointerDown(PointerEventData eventData) {
            onClickDown?.Invoke(eventData , args);
        }
        public void OnPointerUp(PointerEventData eventData) {
            onClickUp?.Invoke(eventData , args);
        }
    }
}
using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace WXUtils.UI
{
    public static class UIExtension
    {
        public static void OnClick(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onClick = action;
            if (null != args) {
                listener.args = args;
            }
        }
        public static void OnClick(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onClick = action;
            if (null != args) {
                listener.args = args;
            }
        }

        public static void OnClickDown(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onClickDown = action;
            if (null != args) {
                listener.args = args;
            }
        }
        public static void OnClickDown(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onClickDown = action;
            if (null != args) {
                listener.args = args;
            }
        }

        public static void OnClickUp(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onClickUp = action;
            if (null != args) {
                listener.args = args;
            }
        }
        public static void OnClickUp(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onClickUp = action;
            if (null != args) {
                listener.args = args;
            }
        }

        public static void OnBeginDrag(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onBeginDrag = action;
            if (null != args) {
                listener.args = args;
            }
        }
        public static void OnBeginDrag(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onBeginDrag = action;
            if (null != args) {
                listener.args = args;
            }
        }

        public static void OnDrag(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onDrag = action;
            if (null != args) {
                listener.args = args;
            }
        }
        public static void OnDrag(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onDrag = action;
            if (null != args) {
                listener.args = args;
            }
        }

        public static void OnEndDrag(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onEndDrag = action;
            if (null != args) {
                listener.args = args;
            }
        }
        public static void OnEndDrag(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
            WXListener listener = go.GetOrAddComponent<WXListener>();
            listener.onEndDrag = action;
            if (null != args) {
                listener.args = args;
            }
        }
    }
    public static class Utils
    {
        public static T GetOrAddComponent<T>(this GameObject go) where T : Component {
            T t = go.GetComponent<T>();
            if (null == t) {
                t = go.AddComponent<T>();
            }
            return t;
        }
        public static T GetOrAddComponent<T>(this Transform go) where T : Component {
            T t = go.GetComponent<T>();
            if (null == t) {
                t = go.gameObject.AddComponent<T>();
            }
            return t;
        }
    }
}
using UnityEngine;
using UnityEngine.EventSystems;
using WXUtils.UI;

public class Test : MonoBehaviour
{
    public GameObject go;
    void Start()
    {
        go.OnClick(Click,go);
        go.OnClickDown(ClickDown , go);
        go.OnClickUp(ClickUp , go);
        go.OnBeginDrag(OnBeginDrag , go);
        go.OnEndDrag(OnEndDrag , go);
        go.OnDrag(Drag , go);
    }

    private void Click(PointerEventData data,object[]args) {
        if (offset==Vector2.zero) {
            Debug.Log((args [0] as GameObject).name+"-- Click");
        }
    }
    private void ClickDown(PointerEventData data , object [] args) {
        Debug.Log((args [0] as GameObject).name+"-- ClickDown");
    }
    private void ClickUp(PointerEventData data , object [] args) {
        Debug.Log((args [0] as GameObject).name + "-- ClickUp");
    }
    Vector2 offset;
    private void OnBeginDrag(PointerEventData data , object [] args) { 
        GameObject value = args [0] as GameObject;
        offset = (Vector2)value.transform.position - data.position;
    }
    private void Drag(PointerEventData data , object [] args) {
        GameObject value = args [0] as GameObject;
        value.transform.position = data.position+offset;
    }
    private void OnEndDrag(PointerEventData data , object [] args) {
        Debug.Log("EndDrag");
        offset = Vector2.zero;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>