/// 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; protected int items = 0; 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; [Tooltip("Button to go to the next page. (optional)")] public GameObject nextButton; [Tooltip("Button to go to the previous page. (optional)")] public GameObject 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; 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 Start() { lerp = false; scrollRect = gameObject.GetComponent (); scrollRectTransform = gameObject.GetComponent (); listContainerTransform = scrollRect.content; listContainerRectTransform = listContainerTransform.GetComponent (); rectTransform = listContainerTransform.gameObject.GetComponent (); UpdateListItemsSize(); UpdateListItemPositions(); ChangePage (CurrentPage ()); if (nextButton) { nextButton.GetComponent