lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

参考链接:

https://blog.csdn.net/lyh916/article/details/44570503

https://blog.csdn.net/qq_38451208/article/details/77979673

 

代码如下:

  1 using UnityEngine;
  2 using UnityEngine.EventSystems;
  3 
  4 public class EventTriggerListener : EventTrigger
  5 {
  6     #region 变量
  7     public delegate void VoidDelegate();
  8     public delegate void PointerEventDelegate(PointerEventData eventData);
  9     public delegate void BaseEventDelegate(BaseEventData eventData);
 10     public delegate void AxisEventDelegate(AxisEventData eventData);
 11 
 12     //不带参
 13     public event VoidDelegate onPointerEnter;
 14     public event VoidDelegate onPointerExit;
 15     public event VoidDelegate onPointerDown;
 16     public event VoidDelegate onPointerUp;
 17     public event VoidDelegate onPointerClick;
 18     public event VoidDelegate onInitializePotentialDrag;
 19     public event VoidDelegate onBeginDrag;
 20     public event VoidDelegate onDrag;
 21     public event VoidDelegate onEndDrag;
 22     public event VoidDelegate onDrop;
 23     public event VoidDelegate onScroll;
 24     public event VoidDelegate onUpdateSelected;
 25     public event VoidDelegate onSelect;
 26     public event VoidDelegate onDeselect;
 27     public event VoidDelegate onMove;
 28     public event VoidDelegate onSubmit;
 29     public event VoidDelegate onCancel;
 30 
 31     //带参
 32     public event PointerEventDelegate onPointerEnterPara;
 33     public event PointerEventDelegate onPointerExitPara;
 34     public event PointerEventDelegate onPointerDownPara;
 35     public event PointerEventDelegate onPointerUpPara;
 36     public event PointerEventDelegate onPointerClickPara;
 37     public event PointerEventDelegate onInitializePotentialDragPara;
 38     public event PointerEventDelegate onBeginDragPara;
 39     public event PointerEventDelegate onDragPara;
 40     public event PointerEventDelegate onEndDragPara;
 41     public event PointerEventDelegate onDropPara;
 42     public event PointerEventDelegate onScrollPara;
 43     public event BaseEventDelegate onUpdateSelectedPara;
 44     public event BaseEventDelegate onSelectPara;
 45     public event BaseEventDelegate onDeselectPara;
 46     public event AxisEventDelegate onMovePara;
 47     public event BaseEventDelegate onSubmitPara;
 48     public event BaseEventDelegate onCancelPara;
 49     #endregion
 50 
 51     public static EventTriggerListener Get(GameObject go)
 52     {
 53         EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
 54         if (listener == null)
 55         {
 56             listener = go.AddComponent<EventTriggerListener>();
 57         }
 58         return listener;
 59     }
 60 
 61     public static void Remove(GameObject go)
 62     {
 63         EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
 64         if (listener != null)
 65         {
 66             Destroy(listener);
 67         }
 68     }
 69 
 70     #region 方法
 71     public override void OnPointerEnter(PointerEventData eventData)
 72     {
 73         //Debug.LogWarning("OnPointerEnter");
 74         
 75         if (onPointerEnter != null)
 76         {
 77             onPointerEnter();
 78         }
 79 
 80         if (onPointerEnterPara != null)
 81         {
 82             onPointerEnterPara(eventData);
 83         }
 84     }
 85 
 86     public override void OnPointerExit(PointerEventData eventData)
 87     {
 88         //Debug.LogWarning("OnPointerExit");
 89 
 90         if (onPointerExit != null)
 91         {
 92             onPointerExit();
 93         }
 94 
 95         if (onPointerExitPara != null)
 96         {
 97             onPointerExitPara(eventData);
 98         }
 99     }
100 
101     public override void OnPointerDown(PointerEventData eventData)
102     {
103         //Debug.LogWarning("OnPointerDown");
104 
105         if (onPointerDown != null)
106         {
107             onPointerDown();
108         }
109 
110         if (onPointerDownPara != null)
111         {
112             onPointerDownPara(eventData);
113         }
114     }
115 
116     public override void OnPointerUp(PointerEventData eventData)
117     {
118         //Debug.LogWarning("OnPointerUp");
119 
120         if (onPointerUp != null)
121         {
122             onPointerUp();
123         }
124 
125         if (onPointerUpPara != null)
126         {
127             onPointerUpPara(eventData);
128         }
129     }
130 
131     public override void OnPointerClick(PointerEventData eventData)
132     {
133         //Debug.LogWarning("OnPointerClick");
134 
135         if (onPointerClick != null)
136         {
137             onPointerClick();
138         }
139 
140         if (onPointerClickPara != null)
141         {
142             onPointerClickPara(eventData);
143         }
144     }
145 
146     public override void OnInitializePotentialDrag(PointerEventData eventData)
147     {
148         //Debug.LogWarning("OnInitializePotentialDrag");
149 
150         if (onInitializePotentialDrag != null)
151         {
152             onInitializePotentialDrag();
153         }
154 
155         if (onInitializePotentialDragPara != null)
156         {
157             onInitializePotentialDragPara(eventData);
158         }
159     }
160 
161     public override void OnBeginDrag(PointerEventData eventData)
162     {
163         //Debug.LogWarning("OnBeginDrag");
164 
165         if (onBeginDrag != null)
166         {
167             onBeginDrag();
168         }
169 
170         if (onBeginDragPara != null)
171         {
172             onBeginDragPara(eventData);
173         }
174     }
175 
176     public override void OnDrag(PointerEventData eventData)
177     {
178         //Debug.LogWarning("OnDrag");
179 
180         if (onDrag != null)
181         {
182             onDrag();
183         }
184 
185         if (onDragPara != null)
186         {
187             onDragPara(eventData);
188         }
189     }
190 
191     public override void OnEndDrag(PointerEventData eventData)
192     {
193         //Debug.LogWarning("OnEndDrag");
194 
195         if (onEndDrag != null)
196         {
197             onEndDrag();
198         }
199 
200         if (onEndDragPara != null)
201         {
202             onEndDragPara(eventData);
203         }
204     }
205 
206     public override void OnDrop(PointerEventData eventData)
207     {
208         //Debug.LogWarning("OnDrop");
209 
210         if (onDrop != null)
211         {
212             onDrop();
213         }
214 
215         if (onDropPara != null)
216         {
217             onDropPara(eventData);
218         }
219     }
220 
221     public override void OnScroll(PointerEventData eventData)
222     {
223         //Debug.LogWarning("OnScroll");
224 
225         if (onScroll != null)
226         {
227             onScroll();
228         }
229 
230         if (onScrollPara != null)
231         {
232             onScrollPara(eventData);
233         }
234     }
235 
236     public override void OnUpdateSelected(BaseEventData eventData)
237     {
238         //Debug.LogWarning("OnUpdateSelected");
239 
240         if (onUpdateSelected != null)
241         {
242             onUpdateSelected();
243         }
244 
245         if (onUpdateSelectedPara != null)
246         {
247             onUpdateSelectedPara(eventData);
248         }
249     }
250 
251     public override void OnSelect(BaseEventData eventData)
252     {
253         //Debug.LogWarning("OnSelect");
254 
255         if (onSelect != null)
256         {
257             onSelect();
258         }
259 
260         if (onSelectPara != null)
261         {
262             onSelectPara(eventData);
263         }
264     }
265 
266     public override void OnDeselect(BaseEventData eventData)
267     {
268         //Debug.LogWarning("OnDeselect");
269 
270         if (onDeselect != null)
271         {
272             onDeselect();
273         }
274 
275         if (onDeselectPara != null)
276         {
277             onDeselectPara(eventData);
278         }
279     }
280 
281     public override void OnMove(AxisEventData eventData)
282     {
283         //Debug.LogWarning("OnMove");
284 
285         if (onMove != null)
286         {
287             onMove();
288         }
289 
290         if (onMovePara != null)
291         {
292             onMovePara(eventData);
293         }
294     }
295 
296     public override void OnSubmit(BaseEventData eventData)
297     {
298         //Debug.LogWarning("OnSubmit");
299 
300         if (onSubmit != null)
301         {
302             onSubmit();
303         }
304 
305         if (onSubmitPara != null)
306         {
307             onSubmitPara(eventData);
308         }
309     }
310 
311     public override void OnCancel(BaseEventData eventData)
312     {
313         //Debug.LogWarning("OnCancel");
314 
315         if (onCancel != null)
316         {
317             onCancel();
318         }
319 
320         if (onCancelPara != null)
321         {
322             onCancelPara(eventData);
323         }
324     }
325     #endregion
326 }

 

测试:

 1 using UnityEngine;
 2 
 3 public class TestEventTriggerListener : MonoBehaviour
 4 {
 5     public GameObject image;
 6 
 7     void Start()
 8     {
 9         EventTriggerListener.Get(image).onPointerClick += Click;
10     }
11 
12     public void Click()
13     {
14         Debug.Log("Click!");
15     }
16 }

 

注意事项:

一.点击

1.点击事件执行顺序:OnPointerDown->OnPointerUp->OnPointerClick

2.OnPointerDown:指针在目标范围内按下时触发

3.OnPointerUp:指针抬起时触发,即使不在目标范围内,也会触发

4.OnPointerClick:指针对目标完成一次点击(按下并抬起)时触发,如果指针抬起时不在目标范围内,则不会触发

二.拖拽

posted on 2019-12-02 23:45  艰苦奋斗中  阅读(441)  评论(0编辑  收藏  举报