From 184f608ed71dd9c3c2f05026f001be942ba03038 Mon Sep 17 00:00:00 2001 From: "Simon (darkside) Jackson" Date: Mon, 22 Jun 2020 15:48:16 +0100 Subject: [PATCH] Switched to using AnchoredPosition as key rather than the original LocalPosition. This seems to resolve the long standing intermittent issue with the content becoming offset on scroll. --- Scripts/Layout/HorizontalScrollSnap.cs | 22 +++++++++++----------- Scripts/Layout/ScrollSnapBase.cs | 17 +++++++++++------ Scripts/Layout/VerticalScrollSnap.cs | 22 +++++++++++----------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index c91b27b..457cb27 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -27,7 +27,7 @@ namespace UnityEngine.UI.Extensions { if (!_settled && !_pointerDown) { - if (!IsRectSettledOnaPage(_screensContainer.localPosition)) + if (!IsRectSettledOnaPage(_screensContainer.anchoredPosition)) { ScrollToClosestElement(); } @@ -36,16 +36,16 @@ namespace UnityEngine.UI.Extensions } else if (_lerp) { - _screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * (UseTimeScale ? Time.deltaTime : Time.unscaledDeltaTime)); - if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f) + _screensContainer.anchoredPosition = Vector3.Lerp(_screensContainer.anchoredPosition, _lerp_target, transitionSpeed * (UseTimeScale ? Time.deltaTime : Time.unscaledDeltaTime)); + if (Vector3.Distance(_screensContainer.anchoredPosition, _lerp_target) < 0.1f) { - _screensContainer.localPosition = _lerp_target; + _screensContainer.anchoredPosition = _lerp_target; _lerp = false; EndScreenChange(); } } - CurrentPage = GetPageforPosition(_screensContainer.localPosition); + CurrentPage = GetPageforPosition(_screensContainer.anchoredPosition); //If the container is moving check if it needs to settle on a page if (!_pointerDown) @@ -196,7 +196,7 @@ namespace UnityEngine.UI.Extensions private void SetScrollContainerPosition() { - _scrollStartPosition = _screensContainer.localPosition.x; + _scrollStartPosition = _screensContainer.anchoredPosition.x; _scroll_rect.horizontalNormalizedPosition = (float)(_currentPage) / (_screens - 1); OnCurrentScreenChange(_currentPage); } @@ -251,7 +251,7 @@ namespace UnityEngine.UI.Extensions } else { - var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition); + var distance = Vector3.Distance(_startPosition, _screensContainer.anchoredPosition); if (UseHardSwipe) { @@ -259,7 +259,7 @@ namespace UnityEngine.UI.Extensions if (distance > FastSwipeThreshold) { - if (_startPosition.x - _screensContainer.localPosition.x > 0) + if (_startPosition.x - _screensContainer.anchoredPosition.x > 0) { NextScreen(); } @@ -278,9 +278,9 @@ namespace UnityEngine.UI.Extensions if (UseFastSwipe && distance < panelDimensions.width && distance >= FastSwipeThreshold) { _scroll_rect.velocity = Vector3.zero; - if (_startPosition.x - _screensContainer.localPosition.x > 0) + if (_startPosition.x - _screensContainer.anchoredPosition.x > 0) { - if (_startPosition.x - _screensContainer.localPosition.x > _childSize / 3) + if (_startPosition.x - _screensContainer.anchoredPosition.x > _childSize / 3) { ScrollToClosestElement(); } @@ -291,7 +291,7 @@ namespace UnityEngine.UI.Extensions } else { - if (_startPosition.x - _screensContainer.localPosition.x < -_childSize / 3) + if (_startPosition.x - _screensContainer.anchoredPosition.x < -_childSize / 3) { ScrollToClosestElement(); } diff --git a/Scripts/Layout/ScrollSnapBase.cs b/Scripts/Layout/ScrollSnapBase.cs index c3ed31b..ac1f302 100644 --- a/Scripts/Layout/ScrollSnapBase.cs +++ b/Scripts/Layout/ScrollSnapBase.cs @@ -8,7 +8,7 @@ using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { - public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IScrollSnap + public class ScrollSnapBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IScrollSnap, IPointerClickHandler { internal Rect panelDimensions; internal RectTransform _screensContainer; @@ -390,13 +390,13 @@ namespace UnityEngine.UI.Extensions _childPos = -_childSize * page; if (_isVertical) { - _infiniteOffset = _screensContainer.localPosition.y < 0 ? -_screensContainer.sizeDelta.y * _infiniteWindow : _screensContainer.sizeDelta.y * _infiniteWindow; + _infiniteOffset = _screensContainer.anchoredPosition.y < 0 ? -_screensContainer.sizeDelta.y * _infiniteWindow : _screensContainer.sizeDelta.y * _infiniteWindow; _infiniteOffset = _infiniteOffset == 0 ? 0 : _infiniteOffset < 0 ? _infiniteOffset - _childSize * _infiniteWindow : _infiniteOffset + _childSize * _infiniteWindow; target.y = _childPos + _scrollStartPosition + _infiniteOffset; } else { - _infiniteOffset = _screensContainer.localPosition.x < 0 ? -_screensContainer.sizeDelta.x * _infiniteWindow : _screensContainer.sizeDelta.x * _infiniteWindow; + _infiniteOffset = _screensContainer.anchoredPosition.x < 0 ? -_screensContainer.sizeDelta.x * _infiniteWindow : _screensContainer.sizeDelta.x * _infiniteWindow; _infiniteOffset = _infiniteOffset == 0 ? 0 : _infiniteOffset < 0 ? _infiniteOffset - _childSize * _infiniteWindow : _infiniteOffset + _childSize * _infiniteWindow; target.x = _childPos + _scrollStartPosition + _infiniteOffset; } @@ -408,7 +408,7 @@ namespace UnityEngine.UI.Extensions internal void ScrollToClosestElement() { _lerp = true; - CurrentPage = GetPageforPosition(_screensContainer.localPosition); + CurrentPage = GetPageforPosition(_screensContainer.anchoredPosition); GetPositionforPage(_currentPage, ref _lerp_target); OnCurrentScreenChange(_currentPage); } @@ -570,7 +570,7 @@ namespace UnityEngine.UI.Extensions _pointerDown = true; _settled = false; StartScreenChange(); - _startPosition = _screensContainer.localPosition; + _startPosition = _screensContainer.anchoredPosition; } /// @@ -593,7 +593,7 @@ namespace UnityEngine.UI.Extensions /// int IScrollSnap.CurrentPage() { - return CurrentPage = GetPageforPosition(_screensContainer.localPosition); + return CurrentPage = GetPageforPosition(_screensContainer.anchoredPosition); } /// @@ -612,6 +612,11 @@ namespace UnityEngine.UI.Extensions GoToScreen(page); } + public void OnPointerClick(PointerEventData eventData) + { + var position = _screensContainer.anchoredPosition; + } + #endregion } } diff --git a/Scripts/Layout/VerticalScrollSnap.cs b/Scripts/Layout/VerticalScrollSnap.cs index 4e28a67..87f7d32 100644 --- a/Scripts/Layout/VerticalScrollSnap.cs +++ b/Scripts/Layout/VerticalScrollSnap.cs @@ -27,7 +27,7 @@ namespace UnityEngine.UI.Extensions { if (!_settled && !_pointerDown) { - if (!IsRectSettledOnaPage(_screensContainer.localPosition)) + if (!IsRectSettledOnaPage(_screensContainer.anchoredPosition)) { ScrollToClosestElement(); } @@ -36,16 +36,16 @@ namespace UnityEngine.UI.Extensions } else if (_lerp) { - _screensContainer.localPosition = Vector3.Lerp(_screensContainer.localPosition, _lerp_target, transitionSpeed * (UseTimeScale ? Time.deltaTime : Time.unscaledDeltaTime)); - if (Vector3.Distance(_screensContainer.localPosition, _lerp_target) < 0.1f) + _screensContainer.anchoredPosition = Vector3.Lerp(_screensContainer.anchoredPosition, _lerp_target, transitionSpeed * (UseTimeScale ? Time.deltaTime : Time.unscaledDeltaTime)); + if (Vector3.Distance(_screensContainer.anchoredPosition, _lerp_target) < 0.1f) { - _screensContainer.localPosition = _lerp_target; + _screensContainer.anchoredPosition = _lerp_target; _lerp = false; EndScreenChange(); } } - CurrentPage = GetPageforPosition(_screensContainer.localPosition); + CurrentPage = GetPageforPosition(_screensContainer.anchoredPosition); //If the container is moving check if it needs to settle on a page if (!_pointerDown) @@ -193,7 +193,7 @@ namespace UnityEngine.UI.Extensions private void SetScrollContainerPosition() { - _scrollStartPosition = _screensContainer.localPosition.y; + _scrollStartPosition = _screensContainer.anchoredPosition.y; _scroll_rect.verticalNormalizedPosition = (float)(_currentPage) / (_screens - 1); OnCurrentScreenChange(_currentPage); } @@ -244,14 +244,14 @@ namespace UnityEngine.UI.Extensions } else { - var distance = Vector3.Distance(_startPosition, _screensContainer.localPosition); + var distance = Vector3.Distance(_startPosition, _screensContainer.anchoredPosition); if (UseHardSwipe) { _scroll_rect.velocity = Vector3.zero; if (distance > FastSwipeThreshold) { - if (_startPosition.y - _screensContainer.localPosition.y > 0) + if (_startPosition.y - _screensContainer.anchoredPosition.y > 0) { NextScreen(); } @@ -270,9 +270,9 @@ namespace UnityEngine.UI.Extensions if (UseFastSwipe && distance < panelDimensions.height + FastSwipeThreshold && distance >= 1f) { _scroll_rect.velocity = Vector3.zero; - if (_startPosition.y - _screensContainer.localPosition.y > 0) + if (_startPosition.y - _screensContainer.anchoredPosition.y > 0) { - if (_startPosition.y - _screensContainer.localPosition.y > _childSize / 3) + if (_startPosition.y - _screensContainer.anchoredPosition.y > _childSize / 3) { ScrollToClosestElement(); } @@ -283,7 +283,7 @@ namespace UnityEngine.UI.Extensions } else { - if (_startPosition.y - _screensContainer.localPosition.y > -_childSize / 3) + if (_startPosition.y - _screensContainer.anchoredPosition.y > -_childSize / 3) { ScrollToClosestElement(); }