diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index 3cdc669..64c5db5 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -2,6 +2,7 @@ /// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1945602 /// Updated by ddreaper - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect. +using System; using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions @@ -246,55 +247,62 @@ namespace UnityEngine.UI.Extensions if (_scroll_rect.horizontal) { - var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition); - - if (UseHardSwipe) + if (UseSwipeDeltaThreshold && Math.Abs(eventData.delta.x) < SwipeDeltaThreshold) { - _scroll_rect.velocity = Vector3.zero; - - if (distance > FastSwipeThreshold) - { - if (_startPosition.x - _screensContainer.localPosition.x > 0) - { - NextScreen(); - } - else - { - PreviousScreen(); - } - } - else - { - ScrollToClosestElement(); - } + ScrollToClosestElement(); } else { - if (UseFastSwipe && distance < panelDimensions.width && distance >= FastSwipeThreshold) + var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition); + + if (UseHardSwipe) { _scroll_rect.velocity = Vector3.zero; - if (_startPosition.x - _screensContainer.localPosition.x > 0) + + if (distance > FastSwipeThreshold) { - if (_startPosition.x - _screensContainer.localPosition.x > _childSize / 3) - { - ScrollToClosestElement(); - } - else + if (_startPosition.x - _screensContainer.localPosition.x > 0) { NextScreen(); } - } - else - { - if (_startPosition.x - _screensContainer.localPosition.x < -_childSize / 3) - { - ScrollToClosestElement(); - } else { PreviousScreen(); } } + else + { + ScrollToClosestElement(); + } + } + else + { + if (UseFastSwipe && distance < panelDimensions.width && distance >= FastSwipeThreshold) + { + _scroll_rect.velocity = Vector3.zero; + if (_startPosition.x - _screensContainer.localPosition.x > 0) + { + if (_startPosition.x - _screensContainer.localPosition.x > _childSize / 3) + { + ScrollToClosestElement(); + } + else + { + NextScreen(); + } + } + else + { + if (_startPosition.x - _screensContainer.localPosition.x < -_childSize / 3) + { + ScrollToClosestElement(); + } + else + { + PreviousScreen(); + } + } + } } } } diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index 4b1a8aa..bf4937a 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -69,12 +69,18 @@ namespace UnityEngine.UI.Extensions [Tooltip("Fast Swipe makes swiping page next / previous (optional)")] public Boolean UseFastSwipe = false; + + [Tooltip("Swipe Delta Threshold looks at the speed of input to decide if a swipe will be initiated (optional)")] + public Boolean UseSwipeDeltaThreshold = false; [Tooltip("Offset for how far a swipe has to travel to initiate a page change (optional)")] public int FastSwipeThreshold = 100; [Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")] public int SwipeVelocityThreshold = 100; + + [Tooltip("Threshold for swipe speed to initiate a swipe, below threshold will return to closest page (optional)")] + public float SwipeDeltaThreshold = 5.0f; [Tooltip("Use time scale instead of unscaled time (optional)")] public Boolean UseTimeScale = true; diff --git a/Scripts/Layout/VerticalScrollSnap.cs b/Scripts/Layout/VerticalScrollSnap.cs index 4c9167c..03285a9 100644 --- a/Scripts/Layout/VerticalScrollSnap.cs +++ b/Scripts/Layout/VerticalScrollSnap.cs @@ -3,6 +3,7 @@ /// Updated by SimonDarksideJ - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect. /// Updated by SimonDarksideJ - major refactoring on updating current position and scroll management +using System; using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions @@ -238,54 +239,62 @@ namespace UnityEngine.UI.Extensions if (_scroll_rect.vertical) { - var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition); - if(UseHardSwipe){ - _scroll_rect.velocity = Vector3.zero; - - if (distance > FastSwipeThreshold) - { - if (_startPosition.y - _screensContainer.localPosition.y > 0) - { - NextScreen(); - } - else - { - PreviousScreen(); - } - } - else - { - ScrollToClosestElement(); - } + if (UseSwipeDeltaThreshold && Math.Abs(eventData.delta.y) < SwipeDeltaThreshold) + { + ScrollToClosestElement(); } else { - if (UseFastSwipe && distance < panelDimensions.height + FastSwipeThreshold && distance >=1f) + var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition); + if (UseHardSwipe) { _scroll_rect.velocity = Vector3.zero; - if (_startPosition.y - _screensContainer.localPosition.y > 0) + + if (distance > FastSwipeThreshold) { - if (_startPosition.y - _screensContainer.localPosition.y > _childSize / 3) - { - ScrollToClosestElement(); - } - else + if (_startPosition.y - _screensContainer.localPosition.y > 0) { NextScreen(); } - } - else - { - if (_startPosition.y - _screensContainer.localPosition.y > -_childSize / 3) - { - ScrollToClosestElement(); - } else { PreviousScreen(); } } - } + else + { + ScrollToClosestElement(); + } + } + else + { + if (UseFastSwipe && distance < panelDimensions.height + FastSwipeThreshold && distance >= 1f) + { + _scroll_rect.velocity = Vector3.zero; + if (_startPosition.y - _screensContainer.localPosition.y > 0) + { + if (_startPosition.y - _screensContainer.localPosition.y > _childSize / 3) + { + ScrollToClosestElement(); + } + else + { + NextScreen(); + } + } + else + { + if (_startPosition.y - _screensContainer.localPosition.y > -_childSize / 3) + { + ScrollToClosestElement(); + } + else + { + PreviousScreen(); + } + } + } + } } } }