EventTrigger.cs
11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.Serialization;
namespace UnityEngine.EventSystems
{
[AddComponentMenu("Event/Event Trigger")]
/// <summary>
/// Receives events from the EventSystem and calls registered functions for each event.
/// </summary>
/// <remarks>
/// The EventTrigger can be used to specify functions you wish to be called for each EventSystem event.
/// You can assign multiple functions to a single event and whenever the EventTrigger receives that event it will call those functions in the order they were provided.
///
/// NOTE: Attaching this component to a GameObject will make that object intercept ALL events, and no events will propagate to parent objects.
/// </remarks>
/// <example>
/// There are two ways to intercept events: You could extend EventTrigger, and override the functions for the events you are interested in intercepting; as shown in this example:
/// <code>
/// using UnityEngine;
/// using UnityEngine.EventSystems;
///
/// public class EventTriggerExample : EventTrigger
/// {
/// public override void OnBeginDrag(PointerEventData data)
/// {
/// Debug.Log("OnBeginDrag called.");
/// }
///
/// public override void OnCancel(BaseEventData data)
/// {
/// Debug.Log("OnCancel called.");
/// }
///
/// public override void OnDeselect(BaseEventData data)
/// {
/// Debug.Log("OnDeselect called.");
/// }
///
/// public override void OnDrag(PointerEventData data)
/// {
/// Debug.Log("OnDrag called.");
/// }
///
/// public override void OnDrop(PointerEventData data)
/// {
/// Debug.Log("OnDrop called.");
/// }
///
/// public override void OnEndDrag(PointerEventData data)
/// {
/// Debug.Log("OnEndDrag called.");
/// }
///
/// public override void OnInitializePotentialDrag(PointerEventData data)
/// {
/// Debug.Log("OnInitializePotentialDrag called.");
/// }
///
/// public override void OnMove(AxisEventData data)
/// {
/// Debug.Log("OnMove called.");
/// }
///
/// public override void OnPointerClick(PointerEventData data)
/// {
/// Debug.Log("OnPointerClick called.");
/// }
///
/// public override void OnPointerDown(PointerEventData data)
/// {
/// Debug.Log("OnPointerDown called.");
/// }
///
/// public override void OnPointerEnter(PointerEventData data)
/// {
/// Debug.Log("OnPointerEnter called.");
/// }
///
/// public override void OnPointerExit(PointerEventData data)
/// {
/// Debug.Log("OnPointerExit called.");
/// }
///
/// public override void OnPointerUp(PointerEventData data)
/// {
/// Debug.Log("OnPointerUp called.");
/// }
///
/// public override void OnScroll(PointerEventData data)
/// {
/// Debug.Log("OnScroll called.");
/// }
///
/// public override void OnSelect(BaseEventData data)
/// {
/// Debug.Log("OnSelect called.");
/// }
///
/// public override void OnSubmit(BaseEventData data)
/// {
/// Debug.Log("OnSubmit called.");
/// }
///
/// public override void OnUpdateSelected(BaseEventData data)
/// {
/// Debug.Log("OnUpdateSelected called.");
/// }
/// }
/// </code>
/// or you can specify individual delegates:
/// <code>
/// using UnityEngine;
/// using UnityEngine.EventSystems;
///
///
/// public class EventTriggerDelegateExample : MonoBehaviour
/// {
/// void Start()
/// {
/// EventTrigger trigger = GetComponent<EventTrigger>();
/// EventTrigger.Entry entry = new EventTrigger.Entry();
/// entry.eventID = EventTriggerType.PointerDown;
/// entry.callback.AddListener((data) => { OnPointerDownDelegate((PointerEventData)data); });
/// trigger.triggers.Add(entry);
/// }
///
/// public void OnPointerDownDelegate(PointerEventData data)
/// {
/// Debug.Log("OnPointerDownDelegate called.");
/// }
/// }
/// </code>
/// </example>
public class EventTrigger :
MonoBehaviour,
IPointerEnterHandler,
IPointerExitHandler,
IPointerDownHandler,
IPointerUpHandler,
IPointerClickHandler,
IInitializePotentialDragHandler,
IBeginDragHandler,
IDragHandler,
IEndDragHandler,
IDropHandler,
IScrollHandler,
IUpdateSelectedHandler,
ISelectHandler,
IDeselectHandler,
IMoveHandler,
ISubmitHandler,
ICancelHandler
{
[Serializable]
/// <summary>
/// UnityEvent class for Triggers.
/// </summary>
public class TriggerEvent : UnityEvent<BaseEventData>
{}
[Serializable]
/// <summary>
/// An Entry in the EventSystem delegates list.
/// </summary>
/// <remarks>
/// It stores the callback and which event type should this callback be fired.
/// </remarks>
public class Entry
{
/// <summary>
/// What type of event is the associated callback listening for.
/// </summary>
public EventTriggerType eventID = EventTriggerType.PointerClick;
/// <summary>
/// The desired TriggerEvent to be Invoked.
/// </summary>
public TriggerEvent callback = new TriggerEvent();
}
[FormerlySerializedAs("delegates")]
[SerializeField]
private List<Entry> m_Delegates;
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[Obsolete("Please use triggers instead (UnityUpgradable) -> triggers", true)]
public List<Entry> delegates { get { return triggers; } set { triggers = value; } }
protected EventTrigger()
{}
/// <summary>
/// All the functions registered in this EventTrigger
/// </summary>
public List<Entry> triggers
{
get
{
if (m_Delegates == null)
m_Delegates = new List<Entry>();
return m_Delegates;
}
set { m_Delegates = value; }
}
private void Execute(EventTriggerType id, BaseEventData eventData)
{
for (int i = 0, imax = triggers.Count; i < imax; ++i)
{
var ent = triggers[i];
if (ent.eventID == id && ent.callback != null)
ent.callback.Invoke(eventData);
}
}
/// <summary>
/// Called by the EventSystem when the pointer enters the object associated with this EventTrigger.
/// </summary>
public virtual void OnPointerEnter(PointerEventData eventData)
{
Execute(EventTriggerType.PointerEnter, eventData);
}
/// <summary>
/// Called by the EventSystem when the pointer exits the object associated with this EventTrigger.
/// </summary>
public virtual void OnPointerExit(PointerEventData eventData)
{
Execute(EventTriggerType.PointerExit, eventData);
}
/// <summary>
/// Called by the EventSystem every time the pointer is moved during dragging.
/// </summary>
public virtual void OnDrag(PointerEventData eventData)
{
Execute(EventTriggerType.Drag, eventData);
}
/// <summary>
/// Called by the EventSystem when an object accepts a drop.
/// </summary>
public virtual void OnDrop(PointerEventData eventData)
{
Execute(EventTriggerType.Drop, eventData);
}
/// <summary>
/// Called by the EventSystem when a PointerDown event occurs.
/// </summary>
public virtual void OnPointerDown(PointerEventData eventData)
{
Execute(EventTriggerType.PointerDown, eventData);
}
/// <summary>
/// Called by the EventSystem when a PointerUp event occurs.
/// </summary>
public virtual void OnPointerUp(PointerEventData eventData)
{
Execute(EventTriggerType.PointerUp, eventData);
}
/// <summary>
/// Called by the EventSystem when a Click event occurs.
/// </summary>
public virtual void OnPointerClick(PointerEventData eventData)
{
Execute(EventTriggerType.PointerClick, eventData);
}
/// <summary>
/// Called by the EventSystem when a Select event occurs.
/// </summary>
public virtual void OnSelect(BaseEventData eventData)
{
Execute(EventTriggerType.Select, eventData);
}
/// <summary>
/// Called by the EventSystem when a new object is being selected.
/// </summary>
public virtual void OnDeselect(BaseEventData eventData)
{
Execute(EventTriggerType.Deselect, eventData);
}
/// <summary>
/// Called by the EventSystem when a new Scroll event occurs.
/// </summary>
public virtual void OnScroll(PointerEventData eventData)
{
Execute(EventTriggerType.Scroll, eventData);
}
/// <summary>
/// Called by the EventSystem when a Move event occurs.
/// </summary>
public virtual void OnMove(AxisEventData eventData)
{
Execute(EventTriggerType.Move, eventData);
}
/// <summary>
/// Called by the EventSystem when the object associated with this EventTrigger is updated.
/// </summary>
public virtual void OnUpdateSelected(BaseEventData eventData)
{
Execute(EventTriggerType.UpdateSelected, eventData);
}
/// <summary>
/// Called by the EventSystem when a drag has been found, but before it is valid to begin the drag.
/// </summary>
public virtual void OnInitializePotentialDrag(PointerEventData eventData)
{
Execute(EventTriggerType.InitializePotentialDrag, eventData);
}
/// <summary>
/// Called before a drag is started.
/// </summary>
public virtual void OnBeginDrag(PointerEventData eventData)
{
Execute(EventTriggerType.BeginDrag, eventData);
}
/// <summary>
/// Called by the EventSystem once dragging ends.
/// </summary>
public virtual void OnEndDrag(PointerEventData eventData)
{
Execute(EventTriggerType.EndDrag, eventData);
}
/// <summary>
/// Called by the EventSystem when a Submit event occurs.
/// </summary>
public virtual void OnSubmit(BaseEventData eventData)
{
Execute(EventTriggerType.Submit, eventData);
}
/// <summary>
/// Called by the EventSystem when a Cancel event occurs.
/// </summary>
public virtual void OnCancel(BaseEventData eventData)
{
Execute(EventTriggerType.Cancel, eventData);
}
}
}