这里主要介绍了Unity 按钮事件封装操作(EventTriggerListener),具有很好的参考价值,废话不多说,直接上代码:$ u6 {' T% c0 I( O6 t
- 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给按钮或者图片添加EventeTrigger事件都需要手动添加组件,或者通过代码继承系统提供接口实现功能。这样使用起来比较麻烦,所以参考网上资料总结写了一个自定义的EventTriggerListener类,专门用于添加事件。
2 j( k, U8 m2 j4 @ EventTriggerListener类,不需要挂载到物体上,继承EventTrigger,作为Common类直接使用:! Z7 }. x+ d9 d, R" l4 t+ W- {
j( V# M- t/ I- 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;
- }
- }
使用方法:新建脚本BtnEvent,继承MonoBehaviour挂载到场景物体中,在Start()周期函数中给需要添加事件的物体添加事件(这里直接挂载到需要注册事件的按钮上)其中gameObject是需要添加的物体gameObject:
5 s5 d1 Z" b5 o* r0 I- _) x' Y-
- 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"); };
- }
- }
运行结果:
; W& }( t3 \1 G
& }* V1 D+ u5 r0 m5 k* p PS:单击和双击事件的同时监听会有冲突,所以通过Invoke()检测300ms内点击次数,每次按钮按下的时候检测。
! x9 p6 O7 T; u/ \ |