首页 > 代码库 > EventListener

EventListener

UGUI的EventSystem事件 

  1 using UnityEngine;
  2 using UnityEngine.EventSystems;
  3 
  4 public class EventListener : EventTrigger
  5 {
  6     public delegate void OnEvent<T>(GameObject go, T eventData);
  7     public OnEvent<PointerEventData> onClick;
  8     public OnEvent<PointerEventData> onUp;
  9     public OnEvent<PointerEventData> onDown;
 10     public OnEvent<PointerEventData> onEnter;
 11     public OnEvent<PointerEventData> onExit;
 12     public OnEvent<PointerEventData> onInitializePotentialDrag;
 13     public OnEvent<PointerEventData> onBeginDrag;
 14     public OnEvent<PointerEventData> onDrop;
 15     public OnEvent<PointerEventData> onDrag;
 16     public OnEvent<PointerEventData> onEndDrag;
 17     public OnEvent<PointerEventData> onScroll;
 18     public OnEvent<BaseEventData> onSelect;
 19     public OnEvent<BaseEventData> onUpdateSelected;
 20     public OnEvent<BaseEventData> onSubmit;
 21     public OnEvent<BaseEventData> onCancel;
 22     public OnEvent<AxisEventData> onMove;
 23     public OnEvent<BaseEventData> onDeselect;
 24     
 25     static public EventListener Get(GameObject go)
 26     {
 27         EventListener listener = go.GetComponent<EventListener>();
 28         if (listener == null) listener = go.AddComponent<EventListener>();
 29         return listener;
 30     }
 31     
 32     //点击事件
 33     public override void OnPointerClick(PointerEventData eventData)
 34     {
 35         if (onClick != null) onClick(gameObject, eventData);
 36     }
 37 
 38     //鼠标抬起事件
 39     public override void OnPointerUp(PointerEventData eventData)
 40     {
 41         if (onUp != null) onUp(gameObject, eventData);
 42     }
 43 
 44     //鼠标按下事件
 45     public override void OnPointerDown(PointerEventData eventData)
 46     {
 47         if (onDown != null) onDown(gameObject, eventData);
 48     }
 49 
 50     //鼠标进入事件
 51     public override void OnPointerEnter(PointerEventData eventData)
 52     {
 53         if (onEnter != null) onEnter(gameObject, eventData);
 54     }
 55 
 56     //输入离开事件
 57     public override void OnPointerExit(PointerEventData eventData)
 58     {
 59         if (onExit != null) onExit(gameObject, eventData);
 60     }
 61 
 62     //可能发生拖拽
 63     public override void OnInitializePotentialDrag(PointerEventData eventData)
 64     {
 65         if (onInitializePotentialDrag != null) onInitializePotentialDrag(gameObject, eventData);
 66     }
 67 
 68     //开始拖拽事件
 69     public override void OnBeginDrag(PointerEventData eventData)
 70     {
 71         if (onBeginDrag != null) onBeginDrag(gameObject, eventData);
 72     }
 73 
 74     //正在拖拽事件
 75     public override void OnDrag(PointerEventData eventData)
 76     {
 77         if (onDrag != null) onDrag(gameObject, eventData);
 78     }
 79 
 80     //拖拽结束【在拖拽结束的对象上发生】
 81     public override void OnDrop(PointerEventData eventData)
 82     {
 83         if (onDrop != null) onDrop(gameObject, eventData);
 84     }
 85 
 86     //结束拖拽事件
 87     public override void OnEndDrag(PointerEventData eventData)
 88     {
 89         if (onEndDrag != null) onEndDrag(gameObject, eventData);
 90     }
 91 
 92     //滚轮事件
 93     public override void OnScroll(PointerEventData eventData)
 94     {
 95         if (onScroll != null) onScroll(gameObject, eventData);
 96     }
 97     
 98     //选中事件
 99     public override void OnSelect(BaseEventData eventData)
100     {
101         if (onSelect != null) onSelect(gameObject, eventData);
102     }
103 
104     //选中事件每帧事件
105     public override void OnUpdateSelected(BaseEventData eventData)
106     {
107         if (onUpdateSelected != null) onUpdateSelected(gameObject, eventData);
108     }
109 
110     //点击Submit键(默认是Enter键)
111     public override void OnSubmit(BaseEventData eventData)
112     {
113         if (onSubmit != null) onSubmit(gameObject, eventData);
114     }
115 
116     //点击Cancel键(默认是Esc键)    
117     public override void OnCancel(BaseEventData eventData)
118     {
119         if (onCancel != null) onCancel(gameObject, eventData);
120     }
121 
122     //点击方向键    
123     public override void OnMove(AxisEventData eventData)
124     {
125         if (onMove != null) onMove(gameObject, eventData);
126     }
127 
128     //不再选中对象
129     public override void OnDeselect(BaseEventData eventData)
130     {
131         if (onDeselect != null) onDeselect(gameObject, eventData);
132     }
133 }

 

EventListener