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"); }; } }
运行结果:
ps:单击和双击事件的同时监听会有冲突,所以通过invoke()检测300ms内点击次数,每次按钮按下的时候检测。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
推荐阅读
-
vant组件中 dialog的确认按钮的回调事件操作
-
unity中点击某一个按钮播放某一个动作的操作
-
iOS-button利用block封装按钮事件【runtime 关联】
-
Unity 按钮事件封装操作(EventTriggerListener)
-
iOS【 列表底部操作按钮视图的封装】(例子: 门店商品列表底部操作按钮视图: 发布商品、批量下/上架、同步网店、打印价签)
-
vant组件中 dialog的确认按钮的回调事件操作
-
Unity通过脚本给按钮动态添加响应事件
-
iOS-button利用block封装按钮事件【runtime 关联】
-
unity中点击某一个按钮播放某一个动作的操作
-
Unity 按钮事件封装操作(EventTriggerListener)