diff --git a/Examples~ b/Examples~ index 9d756f7..906bad3 160000 --- a/Examples~ +++ b/Examples~ @@ -1 +1 @@ -Subproject commit 9d756f7bd9c2195d34e482c9402b715cf760fc96 +Subproject commit 906bad37c84bc2abc156aeef50bcb93b85ea08b3 diff --git a/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs b/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs index aa84881..742788b 100644 --- a/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs +++ b/Runtime/Scripts/Utilities/UI_InfiniteScroll.cs @@ -25,7 +25,7 @@ namespace UnityEngine.UI.Extensions //if true user will need to call Init() method manually (in case the contend of the scrollview is generated from code or requires special initialization) [Tooltip("If false, will Init automatically, otherwise you need to call Init() method")] public bool InitByUser = false; - private ScrollRect _scrollRect; + protected ScrollRect _scrollRect; private ContentSizeFitter _contentSizeFitter; private VerticalLayoutGroup _verticalLayoutGroup; private HorizontalLayoutGroup _horizontalLayoutGroup; @@ -38,7 +38,7 @@ namespace UnityEngine.UI.Extensions protected List items = new List(); private Vector2 _newAnchoredPosition = Vector2.zero; //TO DISABLE FLICKERING OBJECT WHEN SCROLL VIEW IS IDLE IN BETWEEN OBJECTS - private float _treshold = 100f; + private float _threshold = 100f; private int _itemCount = 0; private float _recordOffsetX = 0; private float _recordOffsetY = 0; @@ -179,7 +179,7 @@ namespace UnityEngine.UI.Extensions { if (_isHorizontal) { - if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > _disableMarginX + _treshold) + if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > _disableMarginX + _threshold) { _newAnchoredPosition = items[i].anchoredPosition; _newAnchoredPosition.x -= _itemCount * _recordOffsetX; @@ -197,7 +197,7 @@ namespace UnityEngine.UI.Extensions if (_isVertical) { - if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > _disableMarginY + _treshold) + if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > _disableMarginY + _threshold) { _newAnchoredPosition = items[i].anchoredPosition; _newAnchoredPosition.y -= _itemCount * _recordOffsetY; diff --git a/Runtime/Scripts/Utilities/UI_MagneticInfiniteScroll.cs b/Runtime/Scripts/Utilities/UI_MagneticInfiniteScroll.cs index b28fcd6..0d9b6dc 100644 --- a/Runtime/Scripts/Utilities/UI_MagneticInfiniteScroll.cs +++ b/Runtime/Scripts/Utilities/UI_MagneticInfiniteScroll.cs @@ -5,42 +5,37 @@ using System; using System.Collections; using System.Collections.Generic; +using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { [AddComponentMenu("UI/Extensions/UI Magnetic Infinite Scroll")] - public class UI_MagneticInfiniteScroll : UI_InfiniteScroll + public class UI_MagneticInfiniteScroll : UI_InfiniteScroll, IDragHandler, IEndDragHandler, IScrollHandler { public event Action OnNewSelect; [Tooltip("The pointer to the pivot, the visual element for centering objects.")] [SerializeField] private RectTransform pivot = null; - [Tooltip("The pointer to the object container")] - [SerializeField] - private RectTransform content = null; - [Tooltip("the maximum speed that allows you to activate the magnet to center on the pivot")] + [Tooltip("The maximum speed that allows you to activate the magnet to center on the pivot")] [SerializeField] private float maxSpeedForMagnetic = 10f; [SerializeField] - [Tooltip("The initial index of the object which must be initially centered")] + [Tooltip("The index of the object which must be initially centered")] private int indexStart = 0; [SerializeField] - [Tooltip("The time to decelerate and aim for the pivot")] + [Tooltip("The time to decelerate and aim to the pivot")] private float timeForDeceleration = 0.05f; - [SerializeField] - private ScrollRect scrollRect = null; - private float _pastPositionMouseSpeed; - private float initMovementDirection = 0; + private float _initMovementDirection = 0; private float _pastPosition = 0; private float _currentSpeed = 0.0f; private float _stopValue = 0.0f; private readonly float _waitForContentSet = 0.1f; private float _currentTime = 0; - private int nearestIndex = 0; + private int _nearestIndex = 0; private bool _useMagnetic = true; private bool _isStopping = false; @@ -56,12 +51,12 @@ namespace UnityEngine.UI.Extensions private void Update() { - if (!content || !pivot || !_useMagnetic || !_isMovement || items == null || scrollRect == null) + if (_scrollRect == null || !_scrollRect.content || !pivot || !_useMagnetic || !_isMovement || items == null) { return; } - float currentPosition = GetRightAxis(content.anchoredPosition); + float currentPosition = GetRightAxis(_scrollRect.content.anchoredPosition); _currentSpeed = Mathf.Abs(currentPosition - _pastPosition); _pastPosition = currentPosition; if (Mathf.Abs(_currentSpeed) > maxSpeedForMagnetic) @@ -71,21 +66,21 @@ namespace UnityEngine.UI.Extensions if (_isStopping) { - Vector2 anchoredPosition = content.anchoredPosition; + Vector2 anchoredPosition = _scrollRect.content.anchoredPosition; _currentTime += Time.deltaTime; float valueLerp = _currentTime / timeForDeceleration; float newPosition = Mathf.Lerp(GetRightAxis(anchoredPosition), _stopValue, valueLerp); - content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPosition) : + _scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPosition) : new Vector2(newPosition, anchoredPosition.y); - if (newPosition == GetRightAxis(anchoredPosition) && nearestIndex > 0 && nearestIndex < items.Count) + if (newPosition == GetRightAxis(anchoredPosition) && _nearestIndex > 0 && _nearestIndex < items.Count) { _isStopping = false; _isMovement = false; - var item = items[nearestIndex]; + var item = items[_nearestIndex]; if (item != null && OnNewSelect != null) { @@ -95,7 +90,7 @@ namespace UnityEngine.UI.Extensions } else { - float distance = Mathf.Infinity * (-initMovementDirection); + float distance = Mathf.Infinity * (-_initMovementDirection); for (int i = 0; i < items.Count; i++) { @@ -107,35 +102,20 @@ namespace UnityEngine.UI.Extensions var aux = GetRightAxis(item.position) - GetRightAxis(pivot.position); - if ((initMovementDirection <= 0 && aux < distance && aux > 0) || - (initMovementDirection > 0 && aux > distance && aux < 0)) + if ((_initMovementDirection <= 0 && aux < distance && aux > 0) || + (_initMovementDirection > 0 && aux > distance && aux < 0)) { distance = aux; - nearestIndex = i; + _nearestIndex = i; } } _isStopping = true; - _stopValue = GetAnchoredPositionForPivot(nearestIndex); - scrollRect.StopMovement(); + _stopValue = GetAnchoredPositionForPivot(_nearestIndex); + _scrollRect.StopMovement(); } } - public void Drag() - { - float currentPosition = GetRightAxis(UIExtensionsInputManager.MousePosition); - - initMovementDirection = Mathf.Sign(currentPosition - _pastPositionMouseSpeed); - _pastPositionMouseSpeed = currentPosition; - _useMagnetic = false; - _isStopping = false; - } - - public void EndDrag() - { - FinishPrepareMovement(); - } - public override void SetNewItems(ref List newItems) { foreach (var element in newItems) @@ -149,22 +129,16 @@ namespace UnityEngine.UI.Extensions base.SetNewItems(ref newItems); } - public void Scroll() - { - initMovementDirection = -UIExtensionsInputManager.MouseScrollDelta.y; - FinishPrepareMovement(); - } - public void SetContentInPivot(int index) { float newPos = GetAnchoredPositionForPivot(index); - Vector2 anchoredPosition = content.anchoredPosition; + Vector2 anchoredPosition = _scrollRect.content.anchoredPosition; - if (content) + if (_scrollRect.content) { - content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPos) : + _scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPos) : new Vector2(newPos, anchoredPosition.y); - _pastPosition = GetRightAxis(content.anchoredPosition); + _pastPosition = GetRightAxis(_scrollRect.content.anchoredPosition); } } @@ -200,5 +174,26 @@ namespace UnityEngine.UI.Extensions { return _isVertical ? vector.y : vector.x; } + + public void OnDrag(PointerEventData eventData) + { + float currentPosition = GetRightAxis(UIExtensionsInputManager.MousePosition); + + _initMovementDirection = Mathf.Sign(currentPosition - _pastPositionMouseSpeed); + _pastPositionMouseSpeed = currentPosition; + _useMagnetic = false; + _isStopping = false; + } + + public void OnEndDrag(PointerEventData eventData) + { + FinishPrepareMovement(); + } + + public void OnScroll(PointerEventData eventData) + { + _initMovementDirection = -UIExtensionsInputManager.MouseScrollDelta.y; + FinishPrepareMovement(); + } } }