2020-10-15 01:03:49 +08:00
|
|
|
|
/// Credit Febo Zodiaco
|
|
|
|
|
/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/349/magnticinfinitescroll
|
|
|
|
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2023-01-01 21:29:53 +08:00
|
|
|
|
using UnityEngine.Events;
|
2020-10-20 22:48:07 +08:00
|
|
|
|
using UnityEngine.EventSystems;
|
2020-10-15 01:03:49 +08:00
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
|
{
|
|
|
|
|
[AddComponentMenu("UI/Extensions/UI Magnetic Infinite Scroll")]
|
2020-10-22 06:27:47 +08:00
|
|
|
|
[RequireComponent(typeof(ScrollRect))]
|
2020-10-20 22:48:07 +08:00
|
|
|
|
public class UI_MagneticInfiniteScroll : UI_InfiniteScroll, IDragHandler, IEndDragHandler, IScrollHandler
|
2020-10-15 01:03:49 +08:00
|
|
|
|
{
|
2023-01-01 21:29:53 +08:00
|
|
|
|
public event UnityAction<GameObject> OnNewSelect;
|
2020-10-15 01:03:49 +08:00
|
|
|
|
|
|
|
|
|
[Tooltip("The pointer to the pivot, the visual element for centering objects.")]
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private RectTransform pivot = null;
|
2020-10-20 22:48:07 +08:00
|
|
|
|
[Tooltip("The maximum speed that allows you to activate the magnet to center on the pivot")]
|
2020-10-15 01:03:49 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private float maxSpeedForMagnetic = 10f;
|
|
|
|
|
[SerializeField]
|
2020-10-20 22:48:07 +08:00
|
|
|
|
[Tooltip("The index of the object which must be initially centered")]
|
2020-10-15 01:03:49 +08:00
|
|
|
|
private int indexStart = 0;
|
|
|
|
|
[SerializeField]
|
2020-10-20 22:48:07 +08:00
|
|
|
|
[Tooltip("The time to decelerate and aim to the pivot")]
|
2020-10-15 01:03:49 +08:00
|
|
|
|
private float timeForDeceleration = 0.05f;
|
|
|
|
|
|
|
|
|
|
private float _pastPositionMouseSpeed;
|
2020-10-20 22:48:07 +08:00
|
|
|
|
private float _initMovementDirection = 0;
|
2020-10-15 01:03:49 +08:00
|
|
|
|
private float _pastPosition = 0;
|
|
|
|
|
|
|
|
|
|
private float _currentSpeed = 0.0f;
|
|
|
|
|
private float _stopValue = 0.0f;
|
|
|
|
|
private readonly float _waitForContentSet = 0.1f;
|
|
|
|
|
private float _currentTime = 0;
|
2020-10-20 22:48:07 +08:00
|
|
|
|
private int _nearestIndex = 0;
|
2020-10-15 01:03:49 +08:00
|
|
|
|
|
|
|
|
|
private bool _useMagnetic = true;
|
|
|
|
|
private bool _isStopping = false;
|
|
|
|
|
private bool _isMovement = false;
|
|
|
|
|
|
|
|
|
|
public List<RectTransform> Items { get; }
|
|
|
|
|
|
|
|
|
|
protected override void Awake()
|
|
|
|
|
{
|
|
|
|
|
base.Awake();
|
|
|
|
|
StartCoroutine(SetInitContent());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
2020-10-20 22:48:07 +08:00
|
|
|
|
if (_scrollRect == null || !_scrollRect.content || !pivot || !_useMagnetic || !_isMovement || items == null)
|
2020-10-15 01:03:49 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 22:48:07 +08:00
|
|
|
|
float currentPosition = GetRightAxis(_scrollRect.content.anchoredPosition);
|
2020-10-15 01:03:49 +08:00
|
|
|
|
_currentSpeed = Mathf.Abs(currentPosition - _pastPosition);
|
|
|
|
|
_pastPosition = currentPosition;
|
|
|
|
|
if (Mathf.Abs(_currentSpeed) > maxSpeedForMagnetic)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_isStopping)
|
|
|
|
|
{
|
2020-10-20 22:48:07 +08:00
|
|
|
|
Vector2 anchoredPosition = _scrollRect.content.anchoredPosition;
|
2020-10-15 01:03:49 +08:00
|
|
|
|
_currentTime += Time.deltaTime;
|
|
|
|
|
float valueLerp = _currentTime / timeForDeceleration;
|
|
|
|
|
|
|
|
|
|
float newPosition = Mathf.Lerp(GetRightAxis(anchoredPosition), _stopValue, valueLerp);
|
|
|
|
|
|
2020-10-20 22:48:07 +08:00
|
|
|
|
_scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPosition) :
|
2020-10-15 01:03:49 +08:00
|
|
|
|
new Vector2(newPosition, anchoredPosition.y);
|
|
|
|
|
|
|
|
|
|
|
2020-10-20 22:48:07 +08:00
|
|
|
|
if (newPosition == GetRightAxis(anchoredPosition) && _nearestIndex > 0 && _nearestIndex < items.Count)
|
2020-10-15 01:03:49 +08:00
|
|
|
|
{
|
|
|
|
|
_isStopping = false;
|
|
|
|
|
_isMovement = false;
|
2020-10-20 22:48:07 +08:00
|
|
|
|
var item = items[_nearestIndex];
|
2020-10-15 01:03:49 +08:00
|
|
|
|
if (item != null && OnNewSelect != null)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
OnNewSelect.Invoke(item.gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-10-20 22:48:07 +08:00
|
|
|
|
float distance = Mathf.Infinity * (-_initMovementDirection);
|
2020-10-15 01:03:49 +08:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < items.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var item = items[i];
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var aux = GetRightAxis(item.position) - GetRightAxis(pivot.position);
|
|
|
|
|
|
2020-10-20 22:48:07 +08:00
|
|
|
|
if ((_initMovementDirection <= 0 && aux < distance && aux > 0) ||
|
|
|
|
|
(_initMovementDirection > 0 && aux > distance && aux < 0))
|
2020-10-15 01:03:49 +08:00
|
|
|
|
{
|
|
|
|
|
distance = aux;
|
2020-10-20 22:48:07 +08:00
|
|
|
|
_nearestIndex = i;
|
2020-10-15 01:03:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_isStopping = true;
|
2020-10-20 22:48:07 +08:00
|
|
|
|
_stopValue = GetAnchoredPositionForPivot(_nearestIndex);
|
|
|
|
|
_scrollRect.StopMovement();
|
2020-10-15 01:03:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void SetNewItems(ref List<Transform> newItems)
|
|
|
|
|
{
|
|
|
|
|
foreach (var element in newItems)
|
|
|
|
|
{
|
|
|
|
|
RectTransform rectTransform = element.GetComponent<RectTransform>();
|
|
|
|
|
if (rectTransform && pivot)
|
|
|
|
|
{
|
|
|
|
|
rectTransform.sizeDelta = pivot.sizeDelta;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
base.SetNewItems(ref newItems);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetContentInPivot(int index)
|
|
|
|
|
{
|
|
|
|
|
float newPos = GetAnchoredPositionForPivot(index);
|
2020-10-20 22:48:07 +08:00
|
|
|
|
Vector2 anchoredPosition = _scrollRect.content.anchoredPosition;
|
2020-10-15 01:03:49 +08:00
|
|
|
|
|
2020-10-20 22:48:07 +08:00
|
|
|
|
if (_scrollRect.content)
|
2020-10-15 01:03:49 +08:00
|
|
|
|
{
|
2020-10-20 22:48:07 +08:00
|
|
|
|
_scrollRect.content.anchoredPosition = _isVertical ? new Vector2(anchoredPosition.x, newPos) :
|
2020-10-15 01:03:49 +08:00
|
|
|
|
new Vector2(newPos, anchoredPosition.y);
|
2020-10-20 22:48:07 +08:00
|
|
|
|
_pastPosition = GetRightAxis(_scrollRect.content.anchoredPosition);
|
2020-10-15 01:03:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator SetInitContent()
|
|
|
|
|
{
|
|
|
|
|
yield return new WaitForSeconds(_waitForContentSet);
|
|
|
|
|
SetContentInPivot(indexStart);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float GetAnchoredPositionForPivot(int index)
|
|
|
|
|
{
|
|
|
|
|
if (!pivot || items == null || items.Count < 0)
|
|
|
|
|
{
|
|
|
|
|
return 0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index = Mathf.Clamp(index, 0, items.Count - 1);
|
|
|
|
|
|
|
|
|
|
float posItem = GetRightAxis(items[index].anchoredPosition);
|
|
|
|
|
float posPivot = GetRightAxis(pivot.anchoredPosition);
|
|
|
|
|
return posPivot - posItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FinishPrepareMovement()
|
|
|
|
|
{
|
|
|
|
|
_isMovement = true;
|
|
|
|
|
_useMagnetic = true;
|
|
|
|
|
_isStopping = false;
|
|
|
|
|
_currentTime = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float GetRightAxis(Vector2 vector)
|
|
|
|
|
{
|
|
|
|
|
return _isVertical ? vector.y : vector.x;
|
|
|
|
|
}
|
2020-10-20 22:48:07 +08:00
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2020-10-15 01:03:49 +08:00
|
|
|
|
}
|
2023-01-01 21:29:53 +08:00
|
|
|
|
}
|