|
| 1 | +/// Credit Febo Zodiaco |
| 2 | +/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/349/magnticinfinitescroll |
| 3 | +/// |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections; |
| 7 | +using System.Collections.Generic; |
| 8 | +using UnityEngine.EventSystems; |
| 9 | + |
| 10 | +namespace UnityEngine.UI.Extensions |
| 11 | +{ |
| 12 | + [AddComponentMenu("UI/Extensions/UI Magnetic Infinite Scroll")] |
| 13 | + public class UI_MagneticInfiniteScroll : UI_InfiniteScroll, IDragHandler, IEndDragHandler, IScrollHandler |
| 14 | + { |
| 15 | + public event Action<GameObject> OnNewSelect; |
| 16 | + |
| 17 | + [Tooltip("The pointer to the pivot, the visual element for centering objects.")] |
| 18 | + [SerializeField] |
| 19 | + private RectTransform pivot = null; |
| 20 | + [Tooltip("The maximum speed that allows you to activate the magnet to center on the pivot")] |
| 21 | + [SerializeField] |
| 22 | + private float maxSpeedForMagnetic = 10f; |
| 23 | + [SerializeField] |
| 24 | + [Tooltip("The index of the object which must be initially centered")] |
| 25 | + private int indexStart = 0; |
| 26 | + [SerializeField] |
| 27 | + [Tooltip("The time to decelerate and aim to the pivot")] |
| 28 | + private float timeForDeceleration = 0.05f; |
| 29 | + |
| 30 | + private float _pastPositionMouseSpeed; |
| 31 | + private float _initMovementDirection = 0; |
| 32 | + private float _pastPosition = 0; |
| 33 | + |
| 34 | + private float _currentSpeed = 0.0f; |
| 35 | + private float _stopValue = 0.0f; |
| 36 | + private readonly float _waitForContentSet = 0.1f; |
| 37 | + private float _currentTime = 0; |
| 38 | + private int _nearestIndex = 0; |
| 39 | + |
| 40 | + private bool _useMagnetic = true; |
| 41 | + private bool _isStopping = false; |
| 42 | + private bool _isMovement = false; |
| 43 | + |
| 44 | + public List<RectTransform> Items { get; } |
| 45 | + |
| 46 | + protected override void Awake() |
| 47 | + { |
| 48 | + base.Awake(); |
| 49 | + StartCoroutine(SetInitContent()); |
| 50 | + } |
| 51 | + |
| 52 | + private void Update() |
| 53 | + { |
| 54 | + if (_scrollRect == null || !_scrollRect.content || !pivot || !_useMagnetic || !_isMovement || items == null) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + float currentPosition = GetRightAxis(_scrollRect.content.anchoredPosition); |
| 60 | + _currentSpeed = Mathf.Abs(currentPosition - _pastPosition); |
| 61 | + _pastPosition = currentPosition; |
| 62 | + if (Mathf.Abs(_currentSpeed) > maxSpeedForMagnetic) |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (_isStopping) |
| 68 | + { |
| 69 | + Vector2 anchoredPosition = _scrollRect.content.anchoredPosition; |
| 70 | + _currentTime += Time.deltaTime; |
| 71 | + float valueLerp = _currentTime / timeForDeceleration; |
| 72 | + |
| 73 | + float newPosition = Mathf.Lerp(GetRightAxis(anchoredPosition), _stopValue, valueLerp); |
| 74 | + |
| 75 | + _scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPosition) : |
| 76 | + new Vector2(newPosition, anchoredPosition.y); |
| 77 | + |
| 78 | + |
| 79 | + if (newPosition == GetRightAxis(anchoredPosition) && _nearestIndex > 0 && _nearestIndex < items.Count) |
| 80 | + { |
| 81 | + _isStopping = false; |
| 82 | + _isMovement = false; |
| 83 | + var item = items[_nearestIndex]; |
| 84 | + if (item != null && OnNewSelect != null) |
| 85 | + { |
| 86 | + |
| 87 | + OnNewSelect.Invoke(item.gameObject); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + float distance = Mathf.Infinity * (-_initMovementDirection); |
| 94 | + |
| 95 | + for (int i = 0; i < items.Count; i++) |
| 96 | + { |
| 97 | + var item = items[i]; |
| 98 | + if (item == null) |
| 99 | + { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + var aux = GetRightAxis(item.position) - GetRightAxis(pivot.position); |
| 104 | + |
| 105 | + if ((_initMovementDirection <= 0 && aux < distance && aux > 0) || |
| 106 | + (_initMovementDirection > 0 && aux > distance && aux < 0)) |
| 107 | + { |
| 108 | + distance = aux; |
| 109 | + _nearestIndex = i; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + _isStopping = true; |
| 114 | + _stopValue = GetAnchoredPositionForPivot(_nearestIndex); |
| 115 | + _scrollRect.StopMovement(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public override void SetNewItems(ref List<Transform> newItems) |
| 120 | + { |
| 121 | + foreach (var element in newItems) |
| 122 | + { |
| 123 | + RectTransform rectTransform = element.GetComponent<RectTransform>(); |
| 124 | + if (rectTransform && pivot) |
| 125 | + { |
| 126 | + rectTransform.sizeDelta = pivot.sizeDelta; |
| 127 | + } |
| 128 | + } |
| 129 | + base.SetNewItems(ref newItems); |
| 130 | + } |
| 131 | + |
| 132 | + public void SetContentInPivot(int index) |
| 133 | + { |
| 134 | + float newPos = GetAnchoredPositionForPivot(index); |
| 135 | + Vector2 anchoredPosition = _scrollRect.content.anchoredPosition; |
| 136 | + |
| 137 | + if (_scrollRect.content) |
| 138 | + { |
| 139 | + _scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPos) : |
| 140 | + new Vector2(newPos, anchoredPosition.y); |
| 141 | + _pastPosition = GetRightAxis(_scrollRect.content.anchoredPosition); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private IEnumerator SetInitContent() |
| 146 | + { |
| 147 | + yield return new WaitForSeconds(_waitForContentSet); |
| 148 | + SetContentInPivot(indexStart); |
| 149 | + } |
| 150 | + |
| 151 | + private float GetAnchoredPositionForPivot(int index) |
| 152 | + { |
| 153 | + if (!pivot || items == null || items.Count < 0) |
| 154 | + { |
| 155 | + return 0f; |
| 156 | + } |
| 157 | + |
| 158 | + index = Mathf.Clamp(index, 0, items.Count - 1); |
| 159 | + |
| 160 | + float posItem = GetRightAxis(items[index].anchoredPosition); |
| 161 | + float posPivot = GetRightAxis(pivot.anchoredPosition); |
| 162 | + return posPivot - posItem; |
| 163 | + } |
| 164 | + |
| 165 | + private void FinishPrepareMovement() |
| 166 | + { |
| 167 | + _isMovement = true; |
| 168 | + _useMagnetic = true; |
| 169 | + _isStopping = false; |
| 170 | + _currentTime = 0; |
| 171 | + } |
| 172 | + |
| 173 | + private float GetRightAxis(Vector2 vector) |
| 174 | + { |
| 175 | + return _isVertical ? vector.y : vector.x; |
| 176 | + } |
| 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 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments