C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > Unity按钮事件封装

Unity 按钮事件封装操作(EventTriggerListener)

作者:DaLiangChen

这篇文章主要介绍了Unity 按钮事件封装操作(EventTriggerListener),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

我就废话不多说了,大家还是直接看代码吧~

using UnityEngine;
using UnityEngine.EventSystems; 
namespace Mx.UI
{
    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; 
        /// <summary>
        /// 得到“监听器”组件
        /// </summary>
        /// <param name="go">监听的游戏对象</param>
        /// <returns>
        /// 监听器
        /// </returns>
        public static EventTriggerListener Get(GameObject go)
        {
            EventTriggerListener lister = go.GetComponent<EventTriggerListener>();
            if (lister==null)
            {
                lister = go.AddComponent<EventTriggerListener>();                
            }
            return lister;
        }
 
        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 eventBaseData)
        {
            if (onSelect != null)
            {
                onSelect(gameObject);
            }
        }
 
        public override void OnUpdateSelected(BaseEventData eventBaseData)
        {
            if (onUpdateSelect != null)
            {
                onUpdateSelect(gameObject);
            }
        }	
    }//Class_end
}

补充:Unity学习笔记——自定义EventTriggerListener事件

通常我们在Unity给按钮或者图片添加EventeTrigger事件都需要手动添加组件,或者通过代码继承系统提供接口实现功能。这样使用起来比较麻烦,所以我参考网上资料总结写了一个自定义的EventTriggerListener类,专门用于添加事件。

EventTriggerListener类,不需要挂载到物体上,继承EventTrigger,作为Common类直接使用

using UnityEngine;
using UnityEngine.EventSystems; 
public class EventTriggerListener : EventTrigger
{
    public delegate void voidDelegate(GameObject obj);
    public voidDelegate OnBtnDown;
    public voidDelegate OnBtnUp;
    public voidDelegate OnEnter;
    public voidDelegate OnExit;
    public voidDelegate OnStay;
    public voidDelegate OnClick;
    public voidDelegate OnDoubleClick;
    private float t1, t2;
    private bool isEnter = false;
    public void Update()
    {
        if (isEnter)
        {
            if (OnStay != null)
            {
                OnStay(gameObject);
            }
        }
    }
    public static EventTriggerListener Get(GameObject obj)
    {
        EventTriggerListener eventTriggerListener = obj.GetComponent<EventTriggerListener>();
        if (eventTriggerListener == null)
        {
            eventTriggerListener = obj.AddComponent<EventTriggerListener>();
        }
        return eventTriggerListener;
    }
    /// <summary>
    /// 判断是否是第二次点击
    /// </summary>
    private bool isTwo = true;
    private bool isOver = true;
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (OnBtnDown != null)
        {
            OnBtnDown(gameObject);
        }
        isTwo = !isTwo;
        if (!isOver)
        {
            return;
        }
        isOver = false;
        Invoke("SelectClick", 0.3f);
       
    }
    public void SelectClick()
    {
        if (isTwo)
        {
            if (OnDoubleClick!=null)
            {
                OnDoubleClick(gameObject);
            }
        }
        else
        {
            if (OnClick!=null)
            {
                OnClick(gameObject);
            }
        }
        isTwo = true;
        isOver = true;
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (OnBtnUp != null)
        {
            OnBtnUp(gameObject);
        }
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (OnEnter != null)
        {
            OnEnter(gameObject);
        }
        isEnter = true;
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (OnExit != null)
        {
            OnExit(gameObject);
        }
        isEnter = false;
    }
}

以上是最常见的几种事件,全部事件可以查看EventTrigger类。

使用方法:新建脚本BtnEvent,继承MonoBehaviour挂载到场景物体中,在Start()周期函数中给需要添加事件的物体添加事件(我这里直接挂载到需要注册事件的按钮上)其中gameObject是需要添加的物体gameObject

 
using UnityEngine; 
public class BtnEvent : MonoBehaviour { 
	void Start () {
        EventTriggerListener.Get(transform.gameObject).OnClick += (gameobj) => { Debug.Log("onclick"); };
        EventTriggerListener.Get(transform.gameObject).OnBtnDown += (gameobj) => { Debug.Log("OnBtnDown"); };
        EventTriggerListener.Get(transform.gameObject).OnBtnUp += (gameobj) => { Debug.Log("OnBtnUp"); };
        EventTriggerListener.Get(transform.gameObject).OnEnter += (gameobj) => { Debug.Log("OnEnter"); };
        EventTriggerListener.Get(transform.gameObject).OnExit += (gameobj) => { Debug.Log("OnExit"); };
        EventTriggerListener.Get(transform.gameObject).OnDoubleClick += (gameobj) => { Debug.Log("OnDoubleClick"); };
        EventTriggerListener.Get(transform.gameObject).OnStay += (gameobj) => { Debug.Log("OnStay"); };
    }	
}

运行结果:

PS:单击和双击事件的同时监听会有冲突,所以通过Invoke()检测300ms内点击次数,每次按钮按下的时候检测。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

您可能感兴趣的文章:
阅读全文