/// Credit BinaryX /// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1945602 /// Updated by simonDarksideJ - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect. /// Update by xesenix - rewrote almost the entire code /// - configuration for direction move instead of 2 concurrent class (easier to change direction in editor) /// - supports list layout with horizontal or vertical layout need to match direction with type of layout used /// - dynamic checks if scrolled list size changes and recalculates anchor positions /// and item size based on itemsVisibleAtOnce and size of root container /// if you don't wish to use this auto resize turn of autoLayoutItems /// - fixed current page made it independent from pivot /// - replaced pagination with delegate function using System; using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { [ExecuteInEditMode] [RequireComponent(typeof(ScrollRect))] [AddComponentMenu("UI/Extensions/Scroll Snap")] public class ScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, IScrollSnap { // needed because of reversed behaviour of axis Y compared to X // (positions of children lower in children list in horizontal directions grows when in vertical it gets smaller) public enum ScrollDirection { Horizontal, Vertical } private ScrollRect _scroll_rect; private RectTransform _scrollRectTransform; private Transform _listContainerTransform; //private RectTransform _rectTransform; private int _pages; private int _startingPage = 0; // anchor points to lerp to see child on certain indexes private Vector3[] _pageAnchorPositions; private Vector3 _lerpTarget; private bool _lerp; // item list related private float _listContainerMinPosition; private float _listContainerMaxPosition; private float _listContainerSize; private RectTransform _listContainerRectTransform; private Vector2 _listContainerCachedSize; private float _itemSize; private int _itemsCount = 0; // drag related private bool _startDrag = true; private Vector3 _positionOnDragStart = new Vector3(); private int _pageOnDragStart; private bool _fastSwipeTimer = false; private int _fastSwipeCounter = 0; private int _fastSwipeTarget = 10; [Tooltip("Button to go to the next page. (optional)")] public Button NextButton; [Tooltip("Button to go to the previous page. (optional)")] public Button PrevButton; [Tooltip("Number of items visible in one page of scroll frame.")] [RangeAttribute(1, 100)] public int ItemsVisibleAtOnce = 1; [Tooltip("Sets minimum width of list items to 1/itemsVisibleAtOnce.")] public bool AutoLayoutItems = true; [Tooltip("If you wish to update scrollbar numberOfSteps to number of active children on list.")] public bool LinkScrolbarSteps = false; [Tooltip("If you wish to update scrollrect sensitivity to size of list element.")] public bool LinkScrolrectScrollSensitivity = false; public Boolean UseFastSwipe = true; public int FastSwipeThreshold = 100; public delegate void PageSnapChange(int page); public event PageSnapChange onPageChange; public ScrollDirection direction = ScrollDirection.Horizontal; // Use this for initialization void Start() { _lerp = false; _scroll_rect = gameObject.GetComponent(); _scrollRectTransform = gameObject.GetComponent(); _listContainerTransform = _scroll_rect.content; _listContainerRectTransform = _listContainerTransform.GetComponent(); //_rectTransform = _listContainerTransform.gameObject.GetComponent(); UpdateListItemsSize(); UpdateListItemPositions(); PageChanged(CurrentPage()); if (NextButton) { NextButton.GetComponent