Unity adds a click event to the UI

Add Click Events to UI

Preface

What we're not talking about here is button.click = myClick.Instead, rewrite a click event by inheriting the click event interface
The advantage of this is that it is easy to manage click events and add and remove click events.

thought

The UI is divided into rendering layers, click event layers, and resource loading layers.
First, we need a click event parent, which is used to set up click events of the specified type
Then each event component class inherits the parent class.These include click events, drag events, double-click events, and so on.In this case, only click events are exemplified.
Next comes our UI Layer Component Layer, which is responsible for click events: add, remove event monitoring, trigger events, add click event components
Finally, the event controller.Enumerate all click event types and event components corresponding to all click events.Find event components by event type enumeration, etc.

Code

  • Click Event Base Class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UIEvtCompBase : MonoBehaviour {

   /// <summary>
   ///Event UI
   /// </summary>
   [SerializeField]
   protected UIBaseCommpoent eui;			//The component of the UI, because the component is to be mounted on it
   /// <summary>
   ///Set Event UI
   /// </summary>
   public void SetEUI(UIBaseCommpoent e)
   {
       eui = e;
   }

   /// <summary>
   ///Trigger Event
   /// </summary>
   /// <param name="evtType"></param>
   /// <param name="evtData"></param>
   protected void TriggerUiEvt(UiEvtEnum evtType, BaseEventData evtData)
   {
       if (eui == null)
       {
           eui = GetComponent<UIBaseCommpoent>();
           if (eui == null)
               return;
       }
       eui.DispatchEvent(evtType, evtData);
   }
}
  • Event Component Class
using System;
using UnityEngine.EventSystems;
/// <summary>
///Common Click Event Component
/// </summary>
public class CommonEvtComp : UIEvtCompBase, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
{
   /// <summary>
   ///Click Events
   /// </summary>
   /// <param name="eventData">event data</param>
   public virtual void OnPointerClick(PointerEventData eventData)
   {
       TriggerUiEvt(UiEvtEnum.Click, eventData);
   }
   /// <summary>
   ///Mouse Move In
   /// </summary>
   /// <param name="eventData">event data</param>
   public virtual void OnPointerEnter(PointerEventData eventData)
   {
       TriggerUiEvt(UiEvtEnum.MouseIn, eventData);
   }
   /// <summary>
   ///Mouse out
   /// </summary>
   /// <param name="eventData">event data</param>
   public virtual void OnPointerExit(PointerEventData eventData)
   {
       TriggerUiEvt(UiEvtEnum.MouseOut, eventData);
   }
   /// <summary>
   ///press
   /// </summary>
   /// <param name="eventData"></param>
   public void OnPointerDown(PointerEventData eventData)
   {
       TriggerUiEvt(UiEvtEnum.PointerDown, eventData);
   }
   /// <summary>
   ///Eject
   /// </summary>
   /// <param name="eventData"></param>
   public void OnPointerUp(PointerEventData eventData)
   {
       TriggerUiEvt(UiEvtEnum.PointerUp, eventData);
   }
}
  • UI Event Data Class
public class UIEvtData
{
   public TouchCallback touchCallback;		//This is a callback function for click events, which is written specifically in the event controller
   public System.Object param;				//Parameters passed when clicking on event listening
   public UIEvtData(TouchCallback touchCallback,System.Object param)
   {
       this.touchCallback = touchCallback;
       this.param = param;
   }
   public void CallBack()
   {
       touchCallback(param);
   }
}
  • UI Component Class
/// <summary>
/// UI Click Event Layer
/// </summary>
public class UIBaseCommpoent : MonoBehaviour
{

   /// <summary>
   ///Listened Event Dictionary
   /// </summary>
   protected Dictionary<UiEvtEnum, List<UIEvtData>> uiEvtDic;

   /// <summary>
   ///Event Component Type-Component
   /// </summary>
   protected Dictionary<Type, UIEvtCompBase> uiEvtCompDic;

   //1,

   /// <summary>
   ///Trigger Click Event
   /// </summary>
   /// <param name="evtType"></param>
   /// <param name="evtData"></param>
   public void DispatchEvent(UiEvtEnum evtType, BaseEventData evtData)
   {
       List<UIEvtData> list = null;
       if(uiEvtDic == null)
       {
           uiEvtDic = new Dictionary<UiEvtEnum, List<UIEvtData>>();
       }
       if(uiEvtDic.TryGetValue(evtType,out list))
       {
           foreach (UIEvtData eventItem in list)
           {
               eventItem.CallBack();
           }
       }
   }

   /// <summary>
   ///Add listening events
   /// </summary>
   /// <param name="evtType">Enumeration of event types </param>
   /// <param name="func">callback function</param>
   /// <param name="attachParam">additional parameters</param>
   public virtual void AddEventListener(UiEvtEnum evtType, TouchCallback func, System.Object attachParam = null)
   {
       List<UIEvtData> list = null;
       if (uiEvtDic == null)
       {
           uiEvtDic = new Dictionary<UiEvtEnum, List<UIEvtData>>();
       }
       Type t = UIEventCtrl.GetCompByEvt(evtType);
       if (t != null)
       {
           if (uiEvtCompDic == null)
               uiEvtCompDic = new Dictionary<Type, UIEvtCompBase>();
           UIEvtCompBase comp = null;
           uiEvtCompDic.TryGetValue(t, out comp);
           if(comp == null)
           {
               comp = gameObject.AddComponent(t) as UIEvtCompBase;		//Add Click Event
               uiEvtCompDic.Add(t, comp);														//Click events to add to the dictionary without adding them again
           }
       }
       uiEvtDic.TryGetValue(evtType, out list);									//A button has multiple event listeners, so it is stored in a list
       if (list == null)
       {
           list = new List<UIEvtData>();
           uiEvtDic.Add(evtType, list);
       }
       list.Add(new UIEvtData(func, attachParam));

   }
   /// <summary>
   ///Remove listening events
   /// </summary>
   /// <param name="evtType">Enumeration of event types </param>
   /// <param name="func">callback function</param>
   /// <param name="attachParam">additional parameters</param>
   public virtual void RemoveEventListener(UiEvtEnum evtType, TouchCallback func, System.Object attachParam = null)
   {
       List<UIEvtData> list = null;
       if (uiEvtDic.TryGetValue(evtType, out list))
       {
           for(int i = 0; i < list.Count; i++)
           {
               if (func == list[i].touchCallback)
               {
                   list.RemoveAt(i);
                   break;
               }
           }
       }
   }
}
using System;
/// <summary>
///Event Type Enumeration
/// </summary>
public enum UiEvtEnum
{
   None = 0,
   /// <summary>
   ///Click Events
   /// </summary>
   Click,
   /// <summary>
   ///Mouse Move In Event
   /// </summary>
   MouseIn,
   /// <summary>
   ///Mouse Move Out Event
   /// </summary>
   MouseOut,
   /// <summary>
   ///press
   /// </summary>
   PointerDown,
   /// <summary>
   ///Eject
   /// </summary>
   PointerUp,
   /// <summary>
   ///List (MList) Number of Nodes Change Events
   /// </summary>
   ItemNumChange,
   /// <summary>
   ///List (MList) Node Index Change Events
   /// </summary>
   ItemIndexChange,
   /// <summary>
   ///List (MList) Node Click Event
   /// </summary>
   ItemClick,
   /// <summary>
   ///Data change events
   /// </summary>
   ValueChange,
   /// <summary>
   ///Link Click Event
   /// </summary>
   link,
   /// <summary>
   ///End Event
   /// </summary>
   complete,
   /// <summary>
   ///Start dragging
   /// </summary>
   BeginDrag,
   /// <summary>
   ///Drag
   /// </summary>
   Drag,
   /// <summary>
   ///End dragging
   /// </summary>
   EndDrag,
   /// <summary>
   ///Received drag-and-drop from other controls
   /// </summary>
   ReceiveDragFrom,
   /// <summary>
   ///state change
   /// </summary>
   StateChange,
   /// <summary>
   ///Long press
   /// </summary>
   LongPress,
   /// <summary>
   ///Get Focused
   /// </summary>
   GetFocused,
   /// <summary>
   ///Lose focus
   /// </summary>
   LossFocused
}

/// <summary>
///Click Event Callback
/// </summary>
/// <param name="value">parameter table passed in response </param>
/// <param name="attachParam"></param>
public delegate void TouchCallback(object attachParam);

public class UIEventCtrl {

   /// <summary>
   ///Event Type-Script
   /// </summary>
   public static Type[] uiEvtTypes = new Type[] {
           null
           , typeof(CommonEvtComp)
           , typeof(CommonEvtComp)
           , typeof(CommonEvtComp)
           , typeof(CommonEvtComp)
           , typeof(CommonEvtComp)
           , null
           , null
           , null
           , null
           , null
           , null
           , typeof(DragEvtComp)
           , typeof(DragEvtComp)
           , typeof(DragEvtComp)
           , null
           , null
       };
   /// <summary>
   /// UI Event Management
   /// </summary>
   public UIEventCtrl()
   { }
   /// <summary>
   ///Get the corresponding script type based on the event type
   /// </summary>
   /// <param name="evtType">event type</param>
   /// <returns>script type </returns>
   public static Type GetCompByEvt(UiEvtEnum evtType)
   {
       return uiEvtTypes[(int)evtType];
   }
}

Use

   mImage.AddEventListener(UiEvtEnum.Click, Touch, "Initial data");
   mImage.RemoveEventListener(UiEvtEnum.Drag, Touch);

Posted on Thu, 07 Nov 2019 08:10:29 -0800 by icey37