/// 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.EventSystems; namespace UnityEngine.UI.Extensions { [RequireComponent(typeof(ScrollRect))] [AddComponentMenu("Layout/Extensions/Horizontal Scroll Snap")] public class HorizontalScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler { private Transform _screensContainer; private int _screens = 1; private bool _fastSwipeTimer = false; private int _fastSwipeCounter = 0; private int _fastSwipeTarget = 30; private System.Collections.Generic.List _positions; private ScrollRect _scroll_rect; private Vector3 _lerp_target; private bool _lerp; [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; public Boolean UseFastSwipe = true; public int FastSwipeThreshold = 100; private bool _startDrag = true; private Vector3 _startPosition = new Vector3(); [Tooltip("The currently active page")] private int _currentScreen; [Tooltip("The screen / page to start the control on")] [SerializeField] public int StartingScreen = 1; [Tooltip("The distance between two pages, by default 3 times the width of the control")] [SerializeField] [Range(0, 8)] public float PageStep = 3; public int CurrentPage { get { return _currentScreen; } } // Use this for initialization void Start() { _scroll_rect = gameObject.GetComponent(); if (_scroll_rect.horizontalScrollbar || _scroll_rect.verticalScrollbar) { Debug.LogWarning("Warning, using scrollbors with the Scroll Snap controls is not advised as it causes unpredictable results"); } _screensContainer = _scroll_rect.content; PageStep = (int)_scroll_rect.GetComponent().rect.width * PageStep; DistributePages(); _lerp = false; _currentScreen = StartingScreen; _scroll_rect.horizontalNormalizedPosition = (float)(_currentScreen - 1) / (_screens - 1); ChangeBulletsInfo(_currentScreen); if (NextButton) NextButton.GetComponent