- Home /
Question by
unity_F84B6283DBCBE2CEF602 · Jan 06 at 02:18 PM ·
eventeventsystem
How to capture onBeginDrag() onEndDrag() events for screen,Capture UI events onDrag for screen not for object
Howdy all, I'm new here :}
I'm trying to capture onBeginDrag() onEndDrag() events from EventSystem but having trouble I found this script, I've attached it on MainCamera but nothing happns. I'm trying to capture these events when user swipes screen on mobile. Here's the script code:
using UnityEngine;
using UnityEngine.EventSystems;
using System;
using UnityEngine.Events;
/// <summary>
/// Container class for Serializable Events in UnityEditor.
/// </summary>
public static class Events {
// Additional event-definitions...
[Serializable]
public class Vector2 : UnityEvent<UnityEngine.Vector2> { }
}
public class Swipe : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler {
Vector2 _lastPosition = Vector2.zero;
public Events.Vector2 OnSwipeStart = new Events.Vector2();
public Events.Vector2 OnSwipeEnd = new Events.Vector2();
public Events.Vector2 OnSwipe = new Events.Vector2();
public void OnBeginDrag(PointerEventData eventData) {
_lastPosition = eventData.position;
OnSwipeStart.Invoke(eventData.position);
}
public void OnEndDrag(PointerEventData eventData) {
OnSwipeEnd.Invoke(eventData.position);
}
public void OnDrag(PointerEventData eventData) {
Vector2 direction = eventData.position - _lastPosition;
_lastPosition = eventData.position;
OnSwipe.Invoke(direction);
}
}
,Howdy everybody,
New here :} I'm having trouble captureing onBeginDrag() onEndDrag() UI events for screen. I wan't to implement some logic when user swipes on mobile screen but it doesn't seem to work, I guess I'm not attaching script on right object, here the code:
I attached this script to MainCamera, but nothing happens :/
using System.Diagnostics;
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class Swipe : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler {
Vector2 _lastPosition = Vector2.zero;
public UnityEvent OnSwipeStart;
public UnityEvent OnSwipeEnd;
public UnityEvent OnSwipe;
private void Start() {
if (OnSwipeStart != null) {
//OnSwipeStart.AddListener(Ping);
}
}
public void OnBeginDrag(PointerEventData eventData) {
_lastPosition = eventData.position;
UnityEngine.Debug.Log("OnBeginDrag " + eventData.position);
OnSwipeStart.Invoke();
}
public void OnEndDrag(PointerEventData eventData) {
UnityEngine.Debug.Log("OnEndDrag " + eventData.position);
OnSwipeEnd.Invoke();
}
public void OnDrag(PointerEventData eventData) {
Vector2 direction = eventData.position - _lastPosition;
UnityEngine.Debug.Log("OnDrag " + direction);
_lastPosition = eventData.position;
OnSwipe.Invoke();
}
}
Comment