PointerEventData
How do I use Pointer Event Data
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Cell.cs
Copy
65 public void OnPointerClick(PointerEventData eventData)
66 {
67 if (onCellClick != null)
68 {
69 onCellClick(this);
70 }
71 }
File name: HorizontalScrollSnap.cs
Copy
200 public void OnEndDrag(PointerEventData eventData)
201 {
202 _pointerDown = false;
203
204 if (_scroll_rect.horizontal)
205 {
206 if (UseFastSwipe)
207 {
208 //If using fastswipe - then a swipe does page next / previous
209 if ((_scroll_rect.velocity.x > 0 &&_scroll_rect.velocity.x > FastSwipeThreshold) ||
210 _scroll_rect.velocity.x < 0 && _scroll_rect.velocity.x < -FastSwipeThreshold)
211 {
212 _scroll_rect.velocity = Vector3.zero;
213 if (_startPosition.x - _screensContainer.localPosition.x > 0)
214 {
215 NextScreen();
216 }
217 else
218 {
219 PreviousScreen();
220 }
221 }
222 else
223 {
224 ScrollToClosestElement();
225 }
226 }
227 }
228 }
File name: ScrollSnap.cs
Copy
483 public void OnBeginDrag(PointerEventData eventData)
484 {
485 UpdateScrollbar(false);
486
487 fastSwipeCounter = 0;
488 fastSwipeTimer = true;
489
490 positionOnDragStart = eventData.position;
491 pageOnDragStart = CurrentPage();
492 }
File name: ScrollSnap.cs
Copy
494 public void OnEndDrag(PointerEventData eventData)
495 {
496 startDrag = true;
497 float change = 0;
498
499 if (direction == ScrollDirection.Horizontal)
500 {
501 change = positionOnDragStart.x - eventData.position.x;
502 }
503 else
504 {
505 change = -positionOnDragStart.y + eventData.position.y;
506 }
507
508 if (useFastSwipe)
509 {
510 fastSwipe = false;
511 fastSwipeTimer = false;
512
513 if (fastSwipeCounter <= fastSwipeTarget)
514 {
515 if (Math.Abs(change) > fastSwipeThreshold)
516 {
517 fastSwipe = true;
518 }
519 }
520 if (fastSwipe)
521 {
522 if (change > 0)
523 {
524 NextScreenCommand();
525 }
526 else
527 {
528 PrevScreenCommand();
529 }
530 }
531 else
532 {
533 lerp = true;
534 lerpTarget = pageAnchorPositions[CurrentPage()];
535 }
536 }
537 else
538 {
539 lerp = true;
540 lerpTarget = pageAnchorPositions[CurrentPage()];
541 }
542 }
File name: ScrollSnap.cs
Copy
544 public void OnDrag(PointerEventData eventData)
545 {
546 lerp = false;
547
548 if (startDrag)
549 {
550 OnBeginDrag(eventData);
551 startDrag = false;
552 }
553 }
File name: ScrollSnapBase.cs
Copy
436 public void OnBeginDrag(PointerEventData eventData)
437 {
438 _pointerDown = true;
439 _settled = false;
440 StartScreenChange();
441 _startPosition = _screensContainer.localPosition;
442 }
File name: ScrollSnapBase.cs
Copy
448 public void OnDrag(PointerEventData eventData)
449 {
450 _lerp = false;
451 }
File name: VerticalScrollSnap.cs
Copy
200 public void OnEndDrag(PointerEventData eventData)
201 {
202 _pointerDown = false;
203
204 if (_scroll_rect.vertical)
205 {
206 if (UseFastSwipe)
207 {
208 //If using fastswipe - then a swipe does page next / previous
209 if ((_scroll_rect.velocity.y > 0 && _scroll_rect.velocity.y > FastSwipeThreshold) ||
210 _scroll_rect.velocity.y < 0 && _scroll_rect.velocity.y < -FastSwipeThreshold)
211 {
212 _scroll_rect.velocity = Vector3.zero;
213 if (_startPosition.y - _screensContainer.localPosition.y > 0)
214 {
215 NextScreen();
216 }
217 else
218 {
219 PreviousScreen();
220 }
221 }
222 else
223 {
224 ScrollToClosestElement();
225 }
226 }
227 }
228 }
File name: ScrollSnap.cs
Copy
111 public void OnDrag(PointerEventData data) {
112 float dx = data.delta.x;
113 float dt = Time.deltaTime * 1000f;
114 float acceleration = Mathf.Abs(dx / dt);
115 if(acceleration > triggerAcceleration && acceleration != Mathf.Infinity) {
116 indexChangeTriggered = true;
117 }
118 }
File name: ScrollSnap.cs
Copy
120 public void OnEndDrag(PointerEventData data) {
121 if(IndexShouldChangeFromDrag(data)) {
122 int direction = (data.pressPosition.x - data.position.x) > 0f ? 1 : -1;
123 SnapToIndex(cellIndex + direction);
124 } else {
125 StartLerping();
126 }
127 }
PointerEventData 145 lượt xem
Gõ tìm kiếm nhanh...