diff --git a/Examples/HSS-VSS-ScrollSnap/ScrollSnapManagedTests.unity b/Examples/HSS-VSS-ScrollSnap/ScrollSnapManagedTests.unity index 7c09200..2e78934 100644 --- a/Examples/HSS-VSS-ScrollSnap/ScrollSnapManagedTests.unity +++ b/Examples/HSS-VSS-ScrollSnap/ScrollSnapManagedTests.unity @@ -311,12 +311,12 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: StartingScreen: 0 - PageStep: 2 + PageStep: 2.7 Pagination: {fileID: 266467291} PrevButton: {fileID: 1112321044} NextButton: {fileID: 803284189} transitionSpeed: 7.5 - UseFastSwipe: 0 + UseFastSwipe: 1 FastSwipeThreshold: 100 SwipeVelocityThreshold: 200 MaskArea: {fileID: 0} @@ -1892,7 +1892,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 1, y: 1} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: -113.9, y: -183.2} + m_AnchoredPosition: {x: -113.899994, y: -183.2} m_SizeDelta: {x: 150, y: 30} m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &803284191 diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index 00488a9..5300d33 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -72,7 +72,6 @@ namespace UnityEngine.UI.Extensions int _offset = 0; float _dimension = 0; - Rect panelDimensions = gameObject.GetComponent().rect; float currentXPosition = 0; var pageStepValue = _childSize = (int)panelDimensions.width * ((PageStep == 0) ? 3 : PageStep); @@ -88,7 +87,7 @@ namespace UnityEngine.UI.Extensions _dimension = currentXPosition + _offset * -1; - _screensContainer.GetComponent().offsetMax = new Vector2(_dimension, 0f); + _screensContainer.offsetMax = new Vector2(_dimension, 0f); } /// @@ -214,25 +213,17 @@ namespace UnityEngine.UI.Extensions if (_scroll_rect.horizontal) { - if (UseFastSwipe) + var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition); + if (UseFastSwipe && distance < panelDimensions.width + FastSwipeThreshold) { - //If using fastswipe - then a swipe does page next / previous - if ((_scroll_rect.velocity.x > 0 &&_scroll_rect.velocity.x > FastSwipeThreshold) || - _scroll_rect.velocity.x < 0 && _scroll_rect.velocity.x < -FastSwipeThreshold) + _scroll_rect.velocity = Vector3.zero; + if (_startPosition.x - _screensContainer.localPosition.x > 0) { - _scroll_rect.velocity = Vector3.zero; - if (_startPosition.x - _screensContainer.localPosition.x > 0) - { - NextScreen(); - } - else - { - PreviousScreen(); - } + NextScreen(); } else { - ScrollToClosestElement(); + PreviousScreen(); } } } diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index 6f5e12b..3cd3243 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -10,6 +10,7 @@ namespace UnityEngine.UI.Extensions { public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IScrollSnap { + internal Rect panelDimensions; internal RectTransform _screensContainer; internal bool _isVertical; @@ -29,8 +30,8 @@ namespace UnityEngine.UI.Extensions internal int _currentPage; internal int _previousPage; internal int _halfNoVisibleItems; + internal bool _moveStarted; private int _bottomItem, _topItem; - private bool _moveStarted; [Serializable] public class SelectionChangeStartEvent : UnityEvent { } @@ -63,7 +64,7 @@ namespace UnityEngine.UI.Extensions [Tooltip("Fast Swipe makes swiping page next / previous (optional)")] public Boolean UseFastSwipe = false; - [Tooltip("How far swipe has to travel to initiate a page change (optional)")] + [Tooltip("Offset for how far a swipe has to travel to initiate a page change (optional)\nDefault is the panel dimensions")] public int FastSwipeThreshold = 100; [Tooltip("Speed at which the ScrollRect will keep scrolling before slowing down and stopping (optional)")] @@ -139,7 +140,8 @@ namespace UnityEngine.UI.Extensions var vscroll = _scroll_rect.verticalScrollbar.gameObject.AddComponent(); vscroll.ss = this; } - + panelDimensions = gameObject.GetComponent().rect; + if (StartingScreen < 0) { StartingScreen = 0; diff --git a/Scripts/Layout/VerticalScrollSnap.cs b/Scripts/Layout/VerticalScrollSnap.cs index c150756..06623f0 100644 --- a/Scripts/Layout/VerticalScrollSnap.cs +++ b/Scripts/Layout/VerticalScrollSnap.cs @@ -214,25 +214,17 @@ namespace UnityEngine.UI.Extensions if (_scroll_rect.vertical) { - if (UseFastSwipe) + var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition); + if (UseFastSwipe && distance < panelDimensions.height + FastSwipeThreshold) { - //If using fastswipe - then a swipe does page next / previous - if ((_scroll_rect.velocity.y > 0 && _scroll_rect.velocity.y > FastSwipeThreshold) || - _scroll_rect.velocity.y < 0 && _scroll_rect.velocity.y < -FastSwipeThreshold) + _scroll_rect.velocity = Vector3.zero; + if (_startPosition.y - _screensContainer.localPosition.y > 0) { - _scroll_rect.velocity = Vector3.zero; - if (_startPosition.y - _screensContainer.localPosition.y > 0) - { - NextScreen(); - } - else - { - PreviousScreen(); - } + NextScreen(); } else { - ScrollToClosestElement(); + PreviousScreen(); } } }