欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-18 11:07:34
我就废话不多说了,大家还是直接看代码吧~using unityengine;using unityengine.eventsystems; namespace mx.ui{ public cla...

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

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"); };
    }	
}

运行结果:

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

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

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