namespace UIWidgets { using UnityEngine; using UnityEngine.EventSystems; /// /// Changed drag event to work only with one direction. /// public class DragOneDirection : UIBehaviour, IInitializePotentialDragHandler, IBeginDragHandler, IEndDragHandler, IDragHandler { Vector2 startPosition = Vector2.zero; bool dragging; bool isHorizontal; /// /// Dead zone before /// [SerializeField] public float DeadZone = 20f; /// /// Process begin drag event. /// /// Event data. public void OnBeginDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) { return; } if (!IsActive()) { return; } startPosition = eventData.position; } /// /// Process drag event. /// /// Event data. public void OnDrag(PointerEventData eventData) { if (eventData.button != PointerEventData.InputButton.Left) { return; } if (!IsActive()) { return; } var current = eventData.position; if (dragging) { eventData.position = isHorizontal ? new Vector2(current.x, startPosition.y) : new Vector2(startPosition.x, current.y); } else { var delta = current - startPosition; dragging = delta.magnitude > DeadZone; isHorizontal = Mathf.Abs(delta.x) > Mathf.Abs(delta.y); eventData.position = startPosition; } } /// /// Process end drag event. /// /// Event data. public void OnEndDrag(PointerEventData eventData) { dragging = false; } /// /// Process initialize potential drag event. /// /// Event data. public void OnInitializePotentialDrag(PointerEventData eventData) { } } }