Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 61d0c8e

Browse files
Refined magnetic scroll and dependencies while documenting
Updated example
1 parent 37fa5da commit 61d0c8e

File tree

3 files changed

+49
-54
lines changed

3 files changed

+49
-54
lines changed

Examples~

Submodule Examples~ updated from 9d756f7 to 906bad3

Runtime/Scripts/Utilities/UI_InfiniteScroll.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class UI_InfiniteScroll : MonoBehaviour
2525
//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)
2626
[Tooltip("If false, will Init automatically, otherwise you need to call Init() method")]
2727
public bool InitByUser = false;
28-
private ScrollRect _scrollRect;
28+
protected ScrollRect _scrollRect;
2929
private ContentSizeFitter _contentSizeFitter;
3030
private VerticalLayoutGroup _verticalLayoutGroup;
3131
private HorizontalLayoutGroup _horizontalLayoutGroup;
@@ -38,7 +38,7 @@ public class UI_InfiniteScroll : MonoBehaviour
3838
protected List<RectTransform> items = new List<RectTransform>();
3939
private Vector2 _newAnchoredPosition = Vector2.zero;
4040
//TO DISABLE FLICKERING OBJECT WHEN SCROLL VIEW IS IDLE IN BETWEEN OBJECTS
41-
private float _treshold = 100f;
41+
private float _threshold = 100f;
4242
private int _itemCount = 0;
4343
private float _recordOffsetX = 0;
4444
private float _recordOffsetY = 0;
@@ -179,7 +179,7 @@ public void OnScroll(Vector2 pos)
179179
{
180180
if (_isHorizontal)
181181
{
182-
if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > _disableMarginX + _treshold)
182+
if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > _disableMarginX + _threshold)
183183
{
184184
_newAnchoredPosition = items[i].anchoredPosition;
185185
_newAnchoredPosition.x -= _itemCount * _recordOffsetX;
@@ -197,7 +197,7 @@ public void OnScroll(Vector2 pos)
197197

198198
if (_isVertical)
199199
{
200-
if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > _disableMarginY + _treshold)
200+
if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > _disableMarginY + _threshold)
201201
{
202202
_newAnchoredPosition = items[i].anchoredPosition;
203203
_newAnchoredPosition.y -= _itemCount * _recordOffsetY;

Runtime/Scripts/Utilities/UI_MagneticInfiniteScroll.cs

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,37 @@
55
using System;
66
using System.Collections;
77
using System.Collections.Generic;
8+
using UnityEngine.EventSystems;
89

910
namespace UnityEngine.UI.Extensions
1011
{
1112
[AddComponentMenu("UI/Extensions/UI Magnetic Infinite Scroll")]
12-
public class UI_MagneticInfiniteScroll : UI_InfiniteScroll
13+
public class UI_MagneticInfiniteScroll : UI_InfiniteScroll, IDragHandler, IEndDragHandler, IScrollHandler
1314
{
1415
public event Action<GameObject> OnNewSelect;
1516

1617
[Tooltip("The pointer to the pivot, the visual element for centering objects.")]
1718
[SerializeField]
1819
private RectTransform pivot = null;
19-
[Tooltip("The pointer to the object container")]
20-
[SerializeField]
21-
private RectTransform content = null;
22-
[Tooltip("the maximum speed that allows you to activate the magnet to center on the pivot")]
20+
[Tooltip("The maximum speed that allows you to activate the magnet to center on the pivot")]
2321
[SerializeField]
2422
private float maxSpeedForMagnetic = 10f;
2523
[SerializeField]
26-
[Tooltip("The initial index of the object which must be initially centered")]
24+
[Tooltip("The index of the object which must be initially centered")]
2725
private int indexStart = 0;
2826
[SerializeField]
29-
[Tooltip("The time to decelerate and aim for the pivot")]
27+
[Tooltip("The time to decelerate and aim to the pivot")]
3028
private float timeForDeceleration = 0.05f;
3129

32-
[SerializeField]
33-
private ScrollRect scrollRect = null;
34-
3530
private float _pastPositionMouseSpeed;
36-
private float initMovementDirection = 0;
31+
private float _initMovementDirection = 0;
3732
private float _pastPosition = 0;
3833

3934
private float _currentSpeed = 0.0f;
4035
private float _stopValue = 0.0f;
4136
private readonly float _waitForContentSet = 0.1f;
4237
private float _currentTime = 0;
43-
private int nearestIndex = 0;
38+
private int _nearestIndex = 0;
4439

4540
private bool _useMagnetic = true;
4641
private bool _isStopping = false;
@@ -56,12 +51,12 @@ protected override void Awake()
5651

5752
private void Update()
5853
{
59-
if (!content || !pivot || !_useMagnetic || !_isMovement || items == null || scrollRect == null)
54+
if (_scrollRect == null || !_scrollRect.content || !pivot || !_useMagnetic || !_isMovement || items == null)
6055
{
6156
return;
6257
}
6358

64-
float currentPosition = GetRightAxis(content.anchoredPosition);
59+
float currentPosition = GetRightAxis(_scrollRect.content.anchoredPosition);
6560
_currentSpeed = Mathf.Abs(currentPosition - _pastPosition);
6661
_pastPosition = currentPosition;
6762
if (Mathf.Abs(_currentSpeed) > maxSpeedForMagnetic)
@@ -71,21 +66,21 @@ private void Update()
7166

7267
if (_isStopping)
7368
{
74-
Vector2 anchoredPosition = content.anchoredPosition;
69+
Vector2 anchoredPosition = _scrollRect.content.anchoredPosition;
7570
_currentTime += Time.deltaTime;
7671
float valueLerp = _currentTime / timeForDeceleration;
7772

7873
float newPosition = Mathf.Lerp(GetRightAxis(anchoredPosition), _stopValue, valueLerp);
7974

80-
content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPosition) :
75+
_scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPosition) :
8176
new Vector2(newPosition, anchoredPosition.y);
8277

8378

84-
if (newPosition == GetRightAxis(anchoredPosition) && nearestIndex > 0 && nearestIndex < items.Count)
79+
if (newPosition == GetRightAxis(anchoredPosition) && _nearestIndex > 0 && _nearestIndex < items.Count)
8580
{
8681
_isStopping = false;
8782
_isMovement = false;
88-
var item = items[nearestIndex];
83+
var item = items[_nearestIndex];
8984
if (item != null && OnNewSelect != null)
9085
{
9186

@@ -95,7 +90,7 @@ private void Update()
9590
}
9691
else
9792
{
98-
float distance = Mathf.Infinity * (-initMovementDirection);
93+
float distance = Mathf.Infinity * (-_initMovementDirection);
9994

10095
for (int i = 0; i < items.Count; i++)
10196
{
@@ -107,35 +102,20 @@ private void Update()
107102

108103
var aux = GetRightAxis(item.position) - GetRightAxis(pivot.position);
109104

110-
if ((initMovementDirection <= 0 && aux < distance && aux > 0) ||
111-
(initMovementDirection > 0 && aux > distance && aux < 0))
105+
if ((_initMovementDirection <= 0 && aux < distance && aux > 0) ||
106+
(_initMovementDirection > 0 && aux > distance && aux < 0))
112107
{
113108
distance = aux;
114-
nearestIndex = i;
109+
_nearestIndex = i;
115110
}
116111
}
117112

118113
_isStopping = true;
119-
_stopValue = GetAnchoredPositionForPivot(nearestIndex);
120-
scrollRect.StopMovement();
114+
_stopValue = GetAnchoredPositionForPivot(_nearestIndex);
115+
_scrollRect.StopMovement();
121116
}
122117
}
123118

124-
public void Drag()
125-
{
126-
float currentPosition = GetRightAxis(UIExtensionsInputManager.MousePosition);
127-
128-
initMovementDirection = Mathf.Sign(currentPosition - _pastPositionMouseSpeed);
129-
_pastPositionMouseSpeed = currentPosition;
130-
_useMagnetic = false;
131-
_isStopping = false;
132-
}
133-
134-
public void EndDrag()
135-
{
136-
FinishPrepareMovement();
137-
}
138-
139119
public override void SetNewItems(ref List<Transform> newItems)
140120
{
141121
foreach (var element in newItems)
@@ -149,22 +129,16 @@ public override void SetNewItems(ref List<Transform> newItems)
149129
base.SetNewItems(ref newItems);
150130
}
151131

152-
public void Scroll()
153-
{
154-
initMovementDirection = -UIExtensionsInputManager.MouseScrollDelta.y;
155-
FinishPrepareMovement();
156-
}
157-
158132
public void SetContentInPivot(int index)
159133
{
160134
float newPos = GetAnchoredPositionForPivot(index);
161-
Vector2 anchoredPosition = content.anchoredPosition;
135+
Vector2 anchoredPosition = _scrollRect.content.anchoredPosition;
162136

163-
if (content)
137+
if (_scrollRect.content)
164138
{
165-
content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPos) :
139+
_scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPos) :
166140
new Vector2(newPos, anchoredPosition.y);
167-
_pastPosition = GetRightAxis(content.anchoredPosition);
141+
_pastPosition = GetRightAxis(_scrollRect.content.anchoredPosition);
168142
}
169143
}
170144

@@ -200,5 +174,26 @@ private float GetRightAxis(Vector2 vector)
200174
{
201175
return _isVertical ? vector.y : vector.x;
202176
}
177+
178+
public void OnDrag(PointerEventData eventData)
179+
{
180+
float currentPosition = GetRightAxis(UIExtensionsInputManager.MousePosition);
181+
182+
_initMovementDirection = Mathf.Sign(currentPosition - _pastPositionMouseSpeed);
183+
_pastPositionMouseSpeed = currentPosition;
184+
_useMagnetic = false;
185+
_isStopping = false;
186+
}
187+
188+
public void OnEndDrag(PointerEventData eventData)
189+
{
190+
FinishPrepareMovement();
191+
}
192+
193+
public void OnScroll(PointerEventData eventData)
194+
{
195+
_initMovementDirection = -UIExtensionsInputManager.MouseScrollDelta.y;
196+
FinishPrepareMovement();
197+
}
203198
}
204199
}

0 commit comments

Comments
 (0)