diff --git a/Runtime/Scripts/Layout/HorizontalScrollSnap.cs b/Runtime/Scripts/Layout/HorizontalScrollSnap.cs index 1baacfd..f6de775 100644 --- a/Runtime/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Runtime/Scripts/Layout/HorizontalScrollSnap.cs @@ -27,7 +27,7 @@ namespace UnityEngine.UI.Extensions { updated = false; - if (!_lerp && _scroll_rect.velocity == Vector2.zero) + if (!_lerp && (_scroll_rect.velocity == Vector2.zero && _scroll_rect.inertia)) { if (!_settled && !_pointerDown) { @@ -41,7 +41,7 @@ namespace UnityEngine.UI.Extensions else if (_lerp) { _screensContainer.anchoredPosition = Vector3.Lerp(_screensContainer.anchoredPosition, _lerp_target, transitionSpeed * (UseTimeScale ? Time.deltaTime : Time.unscaledDeltaTime)); - if (Vector3.Distance(_screensContainer.anchoredPosition, _lerp_target) < 0.1f) + if (Vector3.Distance(_screensContainer.anchoredPosition, _lerp_target) < 0.2f) { _screensContainer.anchoredPosition = _lerp_target; _lerp = false; diff --git a/Runtime/Scripts/Layout/ScrollSnapBase.cs b/Runtime/Scripts/Layout/ScrollSnapBase.cs index b8b3087..ff75bae 100644 --- a/Runtime/Scripts/Layout/ScrollSnapBase.cs +++ b/Runtime/Scripts/Layout/ScrollSnapBase.cs @@ -5,6 +5,7 @@ using System; using UnityEngine.Events; using UnityEngine.EventSystems; +using UnityEngine.UIElements; namespace UnityEngine.UI.Extensions { @@ -30,11 +31,13 @@ namespace UnityEngine.UI.Extensions internal int _currentPage; internal int _previousPage; internal int _halfNoVisibleItems; - internal bool _moveStarted; internal bool _isInfinite; // Is a UI Infinite scroller attached to the control internal int _infiniteWindow; // The infinite window the control is in internal float _infiniteOffset; // How much to offset a repositioning - private int _bottomItem, _topItem; + private int _bottomItem, _topItem; + internal bool _startEventCalled = false; + internal bool _endEventCalled = false; + internal bool _suspendEvents = false; [Serializable] public class SelectionChangeStartEvent : UnityEvent { } @@ -443,14 +446,16 @@ namespace UnityEngine.UI.Extensions private void ChangeBulletsInfo(int targetScreen) { if (Pagination) + { for (int i = 0; i < Pagination.transform.childCount; i++) { - Pagination.transform.GetChild(i).GetComponent().isOn = (targetScreen == i) - ? true - : false; + Pagination.transform.GetChild(i).GetComponent().isOn = (targetScreen == i) ? true : false; } + } } + // Make a lock function for pagination, to prevent event leaking + /// /// disables the page navigation buttons when at the first or last screen /// @@ -531,9 +536,12 @@ namespace UnityEngine.UI.Extensions /// public void StartScreenChange() { - if (!_moveStarted) + if (!_startEventCalled) { - _moveStarted = true; + _suspendEvents = true; + + _startEventCalled = true; + _endEventCalled = false; OnSelectionChangeStartEvent.Invoke(); } } @@ -551,9 +559,15 @@ namespace UnityEngine.UI.Extensions /// internal void EndScreenChange() { - OnSelectionChangeEndEvent.Invoke(_currentPage); - _settled = true; - _moveStarted = false; + if (!_endEventCalled) + { + _suspendEvents = false; + + _endEventCalled = true; + _startEventCalled = false; + _settled = true; + OnSelectionChangeEndEvent.Invoke(_currentPage); + } } /// diff --git a/Runtime/Scripts/Layout/VerticalScrollSnap.cs b/Runtime/Scripts/Layout/VerticalScrollSnap.cs index e0c5bb0..58bde93 100644 --- a/Runtime/Scripts/Layout/VerticalScrollSnap.cs +++ b/Runtime/Scripts/Layout/VerticalScrollSnap.cs @@ -228,10 +228,13 @@ namespace UnityEngine.UI.Extensions { InitialiseChildObjectsFromScene(); DistributePages(); - if (MaskArea) UpdateVisible(); + if (MaskArea) + UpdateVisible(); - if (JumpOnEnable || !RestartOnEnable) SetScrollContainerPosition(); - if(RestartOnEnable) GoToScreen(StartingScreen); + if (JumpOnEnable || !RestartOnEnable) + SetScrollContainerPosition(); + if (RestartOnEnable) + GoToScreen(StartingScreen); } /// diff --git a/Runtime/Scripts/Utilities/PaginationManager.cs b/Runtime/Scripts/Utilities/PaginationManager.cs index c80b21f..9f0ca61 100644 --- a/Runtime/Scripts/Utilities/PaginationManager.cs +++ b/Runtime/Scripts/Utilities/PaginationManager.cs @@ -100,7 +100,7 @@ namespace UnityEngine.UI.Extensions { for (int i = 0; i < m_PaginationChildren.Count; i++) { - if (m_PaginationChildren[i].isOn) + if (m_PaginationChildren[i].isOn && !scrollSnap._suspendEvents) { GoToScreen(i); break;