/// Credit BinaryX /// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1945602 /// Updated by ddreaper - removed dependency on a custom ScrollRect script. Now implements drag interfaces and standard Scroll Rect. /// Update by xesenix - rewrited almost entire code /// - configuration for direction move instead of 2 concurrent class (easiear to change direction in editor) /// - supports list layouted with horizontal or vertical layout need to match direction with type of layout used /// - dynamicly checks if scrolled list size changes and recalculates anchor positions /// and item size based on itemsVisibleAtOnce and size of root container /// if you dont wish to use this auto resize turn of autoLayoutItems /// - fixed current page made it independant 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 { // needed becouse of reversed behavior 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 } public delegate void PageSnapChange(int page); public event PageSnapChange onPageChange; public ScrollDirection direction = ScrollDirection.Horizontal; protected ScrollRect scrollRect; protected RectTransform scrollRectTransform; protected Transform listContainerTransform; protected RectTransform rectTransform; int pages; protected int startingPage = 0; // anchor points to lerp to to see child on certain indexes protected Vector3[] pageAnchorPositions; protected Vector3 lerpTarget; protected bool lerp; // item list related protected float listContainerMinPosition; protected float listContainerMaxPosition; protected float listContainerSize; protected RectTransform listContainerRectTransform; protected Vector2 listContainerCachedSize; protected float itemSize; protected int itemsCount = 0; [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; // drag related protected bool startDrag = true; protected Vector3 positionOnDragStart = new Vector3(); protected int pageOnDragStart; protected bool fastSwipeTimer = false; protected int fastSwipeCounter = 0; protected int fastSwipeTarget = 10; // Use this for initialization void Awake() { lerp = false; scrollRect = gameObject.GetComponent(); scrollRectTransform = gameObject.GetComponent(); listContainerTransform = scrollRect.content; listContainerRectTransform = listContainerTransform.GetComponent(); rectTransform = listContainerTransform.gameObject.GetComponent(); UpdateListItemsSize(); UpdateListItemPositions(); PageChanged(CurrentPage()); if (nextButton) { nextButton.GetComponent