/// 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. using System; using UnityEngine.Events; using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { [RequireComponent(typeof(ScrollRect))] [AddComponentMenu("Layout/Extensions/Horizontal Scroll Snap")] public class HorizontalScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler, IPointerDownHandler, IPointerUpHandler { private Transform _screensContainer; private int _screens = 1; private Vector3[] _positions; private Vector3[] _visiblePositions; private ScrollRect _scroll_rect; private Vector3 _lerp_target; private bool _lerp; private bool _pointerDown = false; [Serializable] public class SelectionChangeStartEvent : UnityEvent { } [Serializable] public class SelectionChangeEndEvent : UnityEvent { } [Tooltip("The gameobject that contains toggles which suggest pagination. (optional)")] public GameObject Pagination; [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("Transition speed between pages. (optional)")] public float transitionSpeed = 7.5f; [Tooltip("Fast Swipe makes swiping page next / previous (optional)")] public Boolean UseFastSwipe = false; [Tooltip("How far swipe has to travel to initiate a page change (optional)")] public int FastSwipeThreshold = 100; [Tooltip("How fast can a user swipe to be a swipe (optional)")] public int SwipeVelocityThreshold = 200; private Vector3 _startPosition = new Vector3(); [Tooltip("The currently active page")] private int _currentScreen; private int _previousScreen; [Tooltip("The screen / page to start the control on")] [SerializeField] public int StartingScreen = 1; [Tooltip("The distance between two pages based on page height, by default pages are next to each other")] [SerializeField] [Range(1, 8)] public float PageStep = 1; public int CurrentPage { get { return _currentScreen; } } [SerializeField] private SelectionChangeStartEvent m_OnSelectionChangeStartEvent = new SelectionChangeStartEvent(); public SelectionChangeStartEvent OnSelectionChangeStartEvent { get { return m_OnSelectionChangeStartEvent; } set { m_OnSelectionChangeStartEvent = value; } } [SerializeField] private SelectionChangeEndEvent m_OnSelectionChangeEndEvent = new SelectionChangeEndEvent(); public SelectionChangeEndEvent OnSelectionChangeEndEvent { get { return m_OnSelectionChangeEndEvent; } set { m_OnSelectionChangeEndEvent = value; } } // Use this for initialization void Awake() { _scroll_rect = gameObject.GetComponent(); if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar) { Debug.LogWarning("Warning, using scrollbars with the Scroll Snap controls is not advised as it causes unpredictable results"); } _screensContainer = _scroll_rect.content; DistributePages(); if (NextButton) NextButton.GetComponent