From e1e48e751447e44d2060e07b892ad9b1db3e3ed5 Mon Sep 17 00:00:00 2001 From: "Simon (darkside) Jackson" Date: Thu, 1 Oct 2015 19:29:58 +0100 Subject: [PATCH] Checkin with FlowLayoutGroup, HorrizontalScrollSnap fixes Added the WIP for the new ScollSnap with fixes --HG-- branch : develop_4.6 --- Scripts/Editor/UIExtensionsMenuOptions.cs | 263 ++++++++++++ Scripts/Layout/FlowLayoutGroup.cs | 2 +- Scripts/Layout/HorizontalScrollSnap.cs | 2 +- Scripts/Layout/ScrollSnap.cs | 495 ++++++++++++++++++++++ Scripts/Layout/ScrollSnap.cs.meta | 8 + Scripts/UIScrollToSelectionXY.cs | 127 ++++++ Scripts/UIScrollToSelectionXY.cs.meta | 8 + 7 files changed, 903 insertions(+), 2 deletions(-) create mode 100644 Scripts/Layout/ScrollSnap.cs create mode 100644 Scripts/Layout/ScrollSnap.cs.meta create mode 100644 Scripts/UIScrollToSelectionXY.cs create mode 100644 Scripts/UIScrollToSelectionXY.cs.meta diff --git a/Scripts/Editor/UIExtensionsMenuOptions.cs b/Scripts/Editor/UIExtensionsMenuOptions.cs index fb307be..96d2209 100644 --- a/Scripts/Editor/UIExtensionsMenuOptions.cs +++ b/Scripts/Editor/UIExtensionsMenuOptions.cs @@ -1024,5 +1024,268 @@ namespace UnityEditor.UI } #endregion + + #region New ScrollSnapCode + static public void FixedScrollSnapBase(MenuCommand menuCommand, string name, ScrollSnap.ScrollDirection direction, int itemVisible, int itemCount, Vector2 itemSize) + + { + + GameObject scrollSnapRoot = CreateUIElementRoot(name, menuCommand, s_ThickGUIElementSize); + + + + GameObject itemList = CreateUIObject("List", scrollSnapRoot); + + + + // Set RectTransform to stretch + + RectTransform rectTransformScrollSnapRoot = scrollSnapRoot.GetComponent(); + + rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); + + rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); + + rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; + + + + if (direction == ScrollSnap.ScrollDirection.Horizontal) + + { + + rectTransformScrollSnapRoot.sizeDelta = new Vector2(itemVisible * itemSize.x, itemSize.y); + + } + + else + + { + + rectTransformScrollSnapRoot.sizeDelta = new Vector2(itemSize.x, itemVisible * itemSize.y); + + } + + + + Image image = scrollSnapRoot.AddComponent(); + + image.sprite = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpriteResourcePath); + + image.type = Image.Type.Sliced; + + image.color = new Color(1f, 1f, 1f, 1f); + + + + Mask listMask = scrollSnapRoot.AddComponent(); + + listMask.showMaskGraphic = false; + + + + ScrollRect scrollRect = scrollSnapRoot.AddComponent(); + + scrollRect.vertical = direction == ScrollSnap.ScrollDirection.Vertical; + + scrollRect.horizontal = direction == ScrollSnap.ScrollDirection.Horizontal; + + + + ScrollSnap scrollSnap = scrollSnapRoot.AddComponent(); + + scrollSnap.direction = direction; + + scrollSnap.itemsVisibleAtOnce = itemVisible; + + + + //Setup Content container + + RectTransform rectTransformContent = itemList.GetComponent(); + + rectTransformContent.anchorMin = Vector2.zero; + + rectTransformContent.anchorMax = new Vector2(1f, 1f); + + //rectTransformContent.anchoredPosition = Vector2.zero; + + rectTransformContent.sizeDelta = Vector2.zero; + + scrollRect.content = rectTransformContent; + + + + //Setup Item list container + + if (direction == ScrollSnap.ScrollDirection.Horizontal) + + { + + itemList.AddComponent (); + + ContentSizeFitter sizeFitter = itemList.AddComponent(); + + sizeFitter.horizontalFit = ContentSizeFitter.FitMode.MinSize; + + } + + else + + { + + itemList.AddComponent (); + + ContentSizeFitter sizeFitter = itemList.AddComponent(); + + sizeFitter.verticalFit = ContentSizeFitter.FitMode.MinSize; + + } + + + + //Setup children + + + + for (var i = 0; i < itemCount; i ++) + + { + + GameObject item = CreateUIObject (string.Format("Item_{0:00}", i), itemList); + + + + GameObject childText = CreateUIObject ("Text", item); + + + + Image pageImage = item.AddComponent (); + + pageImage.sprite = AssetDatabase.GetBuiltinExtraResource (kStandardSpritePath); + + pageImage.type = Image.Type.Sliced; + + pageImage.color = s_DefaultSelectableColor; + + + + LayoutElement elementLayout = item.AddComponent (); + + if (direction == ScrollSnap.ScrollDirection.Horizontal) + + { + + elementLayout.minWidth = itemSize.x; + + } + + else + + { + + elementLayout.minHeight = itemSize.y; + + } + + + + RectTransform rectTransformPage01 = item.GetComponent (); + + rectTransformPage01.anchorMin = new Vector2 (0f, 0.5f); + + rectTransformPage01.anchorMax = new Vector2 (0f, 0.5f); + + //rectTransformPage01.anchoredPosition = Vector2.zero; + + //rectTransformPage01.sizeDelta = Vector2.zero; + + rectTransformPage01.pivot = new Vector2 (0f, 0.5f); + + + + //Setup Text on Page01 + + Text text = childText.AddComponent (); + + text.text = item.name; + + text.alignment = TextAnchor.MiddleCenter; + + text.color = new Color (0.196f, 0.196f, 0.196f); + + + + //Setup Text 1st Child + + RectTransform rectTransformPage01Text = childText.GetComponent (); + + rectTransformPage01Text.anchorMin = new Vector2 (0.5f, 0.5f); + + rectTransformPage01Text.anchorMax = new Vector2 (0.5f, 0.5f); + + //rectTransformPage01Text.anchoredPosition = Vector2.zero; + + //rectTransformPage01Text.sizeDelta = Vector2.zero; + + rectTransformPage01Text.pivot = new Vector2 (0.5f, 0.5f); + + } + + + + Selection.activeGameObject = scrollSnapRoot; + + } + + + + [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Horizontal Single Item", false)] + + static public void AddFixedItemScrollSnapHorizontalSingle(MenuCommand menuCommand) + + { + + FixedScrollSnapBase (menuCommand, "Scroll Snap Horizontal Single", ScrollSnap.ScrollDirection.Horizontal, 1, 3, new Vector2(100, 100)); + + } + + + + [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Horizontal Multiple Items", false)] + + static public void AddFixedItemScrollSnapHorizontalMultiple(MenuCommand menuCommand) + + { + + FixedScrollSnapBase (menuCommand, "Scroll Snap Horizontal Multiple", ScrollSnap.ScrollDirection.Horizontal, 3, 15, new Vector2(100, 100)); + + } + + + + [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Vertical Single Item", false)] + + static public void AddFixedItemScrollSnapVerticalSingle(MenuCommand menuCommand) + + { + + FixedScrollSnapBase (menuCommand, "Scroll Snap Vertical Multiple", ScrollSnap.ScrollDirection.Vertical, 1, 3, new Vector2(100, 100)); + + } + + + + [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Vertical Multiple Items", false)] + + static public void AddFixedItemScrollSnapVerticalMultiple(MenuCommand menuCommand) + + { + + FixedScrollSnapBase (menuCommand, "Scroll Snap Vertical Multiple", ScrollSnap.ScrollDirection.Vertical, 3, 15, new Vector2(100, 100)); + + } + + #endregion } } diff --git a/Scripts/Layout/FlowLayoutGroup.cs b/Scripts/Layout/FlowLayoutGroup.cs index 3d4c050..62886fd 100644 --- a/Scripts/Layout/FlowLayoutGroup.cs +++ b/Scripts/Layout/FlowLayoutGroup.cs @@ -165,7 +165,7 @@ namespace UnityEngine.UI.Extensions var h = CalculateRowVerticalOffset(groupHeight, yOffset, currentRowHeight); currentRowWidth -= SpacingX; // Layout the final row - LayoutRow(_rowList, currentRowWidth, currentRowHeight, workingWidth, padding.left, h, axis); + LayoutRow(_rowList, currentRowWidth, currentRowHeight, workingWidth - (_rowList.Count > 1 ? SpacingX : 0), padding.left, h, axis); } _rowList.Clear(); diff --git a/Scripts/Layout/HorizontalScrollSnap.cs b/Scripts/Layout/HorizontalScrollSnap.cs index d2e6f2a..d15d3d4 100644 --- a/Scripts/Layout/HorizontalScrollSnap.cs +++ b/Scripts/Layout/HorizontalScrollSnap.cs @@ -211,7 +211,7 @@ namespace UnityEngine.UI.Extensions RectTransform child = _screensContainer.transform.GetChild(i).gameObject.GetComponent(); currentXPosition = _offset + i * _step; child.anchoredPosition = new Vector2(currentXPosition, 0f); - child.sizeDelta = new Vector2(gameObject.GetComponent().sizeDelta.x, gameObject.GetComponent().sizeDelta.y); + child.sizeDelta = new Vector2(gameObject.GetComponent().rect.width, gameObject.GetComponent().rect.height); } _dimension = currentXPosition + _offset * -1; diff --git a/Scripts/Layout/ScrollSnap.cs b/Scripts/Layout/ScrollSnap.cs new file mode 100644 index 0000000..849f521 --- /dev/null +++ b/Scripts/Layout/ScrollSnap.cs @@ -0,0 +1,495 @@ +/// 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