using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Events; using UnityEngine.UI; namespace UIWidgets { [RequireComponent(typeof(ScrollRect))] public class ScrollRectEvents : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IEventSystemHandler { [SerializeField] public float RequiredMovement = 50f; [SerializeField] public UnityEvent OnPullUp = new UnityEvent(); [SerializeField] public UnityEvent OnPullDown = new UnityEvent(); [SerializeField] public UnityEvent OnPullLeft = new UnityEvent(); [SerializeField] public UnityEvent OnPullRight = new UnityEvent(); private ScrollRect scrollRect; private bool initedPullUp; private bool initedPullDown; private bool initedPullLeft; private bool initedPullRight; private float MovementUp; private float MovementDown; private float MovementLeft; private float MovementRight; public ScrollRect ScrollRect { get { if (scrollRect == null) { scrollRect = GetComponent(); } return scrollRect; } } public virtual void OnBeginDrag(PointerEventData eventData) { initedPullUp = false; initedPullDown = false; initedPullLeft = false; initedPullRight = false; MovementUp = 0f; MovementDown = 0f; MovementLeft = 0f; MovementRight = 0f; } public virtual void OnEndDrag(PointerEventData eventData) { initedPullUp = false; initedPullDown = false; initedPullLeft = false; initedPullRight = false; MovementUp = 0f; MovementDown = 0f; MovementLeft = 0f; MovementRight = 0f; } public virtual void OnDrag(PointerEventData eventData) { RectTransform rectTransform = ScrollRect.transform as RectTransform; float height = rectTransform.rect.height; float width = rectTransform.rect.width; float num = Mathf.Max(0f, ScrollRect.content.rect.height - height); float num2 = Mathf.Max(0f, ScrollRect.content.rect.width - width); if (ScrollRect.content.anchoredPosition.y <= 0f && !initedPullUp) { MovementUp += 0f - eventData.delta.y; if (MovementUp >= RequiredMovement) { initedPullUp = true; OnPullUp.Invoke(); } } if (ScrollRect.content.anchoredPosition.y >= num && !initedPullDown) { MovementDown += eventData.delta.y; if (MovementDown >= RequiredMovement) { initedPullDown = true; OnPullDown.Invoke(); } } if (ScrollRect.content.anchoredPosition.x <= 0f && !initedPullLeft) { MovementLeft += 0f - eventData.delta.x; if (MovementLeft >= RequiredMovement) { initedPullLeft = true; OnPullLeft.Invoke(); } } if (ScrollRect.content.anchoredPosition.x >= num2 && !initedPullRight) { MovementRight += eventData.delta.x; if (MovementRight >= RequiredMovement) { initedPullRight = true; OnPullRight.Invoke(); } } } } }