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內點擊次數,每次按鈕按下的時候檢測。

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀:

    None Found