From b6f4e5a144a337ec83474b14a98d6dbe8f0c78ab Mon Sep 17 00:00:00 2001 From: "Simon (darkside) Jackson" Date: Sat, 17 Oct 2015 16:16:23 +0100 Subject: [PATCH] Updated ScrollSnap with fixes from author --HG-- branch : develop_4.6 --- Scripts/Layout/ScrollSnap.cs | 335 +++++++++++++++++++++-------------- 1 file changed, 199 insertions(+), 136 deletions(-) diff --git a/Scripts/Layout/ScrollSnap.cs b/Scripts/Layout/ScrollSnap.cs index 849f521..ff193e3 100644 --- a/Scripts/Layout/ScrollSnap.cs +++ b/Scripts/Layout/ScrollSnap.cs @@ -9,23 +9,23 @@ /// 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 + [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 { + public enum ScrollDirection + { Horizontal, Vertical - }; + } public delegate void PageSnapChange(int page); @@ -34,11 +34,12 @@ namespace UnityEngine.UI.Extensions public ScrollDirection direction = ScrollDirection.Horizontal; protected ScrollRect scrollRect; - protected RectTransform scrollRectTransform; - protected Transform listContainerTransform; - protected RectTransform rectTransform; - protected int items = 0; + protected RectTransform scrollRectTransform; + + protected Transform listContainerTransform; + + protected RectTransform rectTransform; int pages; @@ -46,22 +47,31 @@ namespace UnityEngine.UI.Extensions // 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 GameObject nextButton; - [Tooltip("Button to go to the previous page. (optional)")] - public GameObject prevButton; + [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)] @@ -72,74 +82,97 @@ namespace UnityEngine.UI.Extensions [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 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() + // Use this for initialization + void Awake() { lerp = false; - scrollRect = gameObject.GetComponent (); - scrollRectTransform = gameObject.GetComponent (); + scrollRect = gameObject.GetComponent(); + scrollRectTransform = gameObject.GetComponent(); listContainerTransform = scrollRect.content; - listContainerRectTransform = listContainerTransform.GetComponent (); + listContainerRectTransform = listContainerTransform.GetComponent(); - rectTransform = listContainerTransform.gameObject.GetComponent (); + rectTransform = listContainerTransform.gameObject.GetComponent(); UpdateListItemsSize(); UpdateListItemPositions(); - ChangePage (CurrentPage ()); + ChangePage(CurrentPage()); if (nextButton) { - nextButton.GetComponent