diff --git a/Scripts/Editor/UIExtensionsMenuOptions.cs b/Scripts/Editor/UIExtensionsMenuOptions.cs index 330d2a8..85107cb 100644 --- a/Scripts/Editor/UIExtensionsMenuOptions.cs +++ b/Scripts/Editor/UIExtensionsMenuOptions.cs @@ -233,6 +233,7 @@ namespace UnityEditor.UI ScrollRect sr = horizontalScrollSnapRoot.AddComponent(); sr.vertical = false; + sr.horizontal = true; horizontalScrollSnapRoot.AddComponent(); //Setup Content container @@ -277,6 +278,78 @@ namespace UnityEditor.UI Selection.activeGameObject = horizontalScrollSnapRoot; } + [MenuItem("GameObject/UI/Extensions/Vertical Scroll Snap", false)] + static public void AddVerticallScrollSnap(MenuCommand menuCommand) + { + GameObject verticalScrollSnapRoot = CreateUIElementRoot("Vertical Scroll Snap", menuCommand, s_ThickGUIElementSize); + + GameObject childContent = CreateUIObject("Content", verticalScrollSnapRoot); + + GameObject childPage01 = CreateUIObject("Page_01", childContent); + + GameObject childText = CreateUIObject("Text", childPage01); + + // Set RectTransform to stretch + RectTransform rectTransformScrollSnapRoot = verticalScrollSnapRoot.GetComponent(); + rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; + rectTransformScrollSnapRoot.sizeDelta = new Vector2(300f, 150f); + + + Image image = verticalScrollSnapRoot.AddComponent(); + image.sprite = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpriteResourcePath); + image.type = Image.Type.Sliced; + image.color = new Color(1f, 1f, 1f, 0.392f); + + ScrollRect sr = verticalScrollSnapRoot.AddComponent(); + sr.vertical = true; + sr.horizontal = false; + verticalScrollSnapRoot.AddComponent(); + + //Setup Content container + RectTransform rectTransformContent = childContent.GetComponent(); + rectTransformContent.anchorMin = Vector2.zero; + rectTransformContent.anchorMax = new Vector2(1f, 1f); + //rectTransformContent.anchoredPosition = Vector2.zero; + rectTransformContent.sizeDelta = Vector2.zero; + + sr.content = rectTransformContent; + + //Setup 1st Child + Image pageImage = childPage01.AddComponent(); + pageImage.sprite = AssetDatabase.GetBuiltinExtraResource(kStandardSpritePath); + pageImage.type = Image.Type.Sliced; + pageImage.color = s_DefaultSelectableColor; + + RectTransform rectTransformPage01 = childPage01.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 = "Page_01"; + 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); + + + //Need to add example child components like in the Asset (SJ) + + Selection.activeGameObject = verticalScrollSnapRoot; + } + + [MenuItem("GameObject/UI/Extensions/UI Button", false)] static public void AddUIButton(MenuCommand menuCommand) { diff --git a/Scripts/VerticalScrollSnap.cs b/Scripts/VerticalScrollSnap.cs new file mode 100644 index 0000000..44ead39 --- /dev/null +++ b/Scripts/VerticalScrollSnap.cs @@ -0,0 +1,285 @@ +/// 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("UI/Extensions/Vertical Scroll Snap")] + public class VerticalScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler + { + private Transform _screensContainer; + + private int _screens = 1; + private int _startingScreen = 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; + + private int _containerSize; + + [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; + + public Boolean UseFastSwipe = true; + public int FastSwipeThreshold = 100; + + private bool _startDrag = true; + private Vector3 _startPosition = new Vector3(); + private int _currentScreen; + + // Use this for initialization + void Start() + { + _scroll_rect = gameObject.GetComponent(); + _screensContainer = _scroll_rect.content; + DistributePages(); + + _screens = _screensContainer.childCount; + + _lerp = false; + + _positions = new System.Collections.Generic.List(); + + if (_screens > 0) + { + for (int i = 0; i < _screens; ++i) + { + _scroll_rect.verticalNormalizedPosition = (float)i / (float)(_screens - 1); + _positions.Add(_screensContainer.localPosition); + } + } + + _scroll_rect.verticalNormalizedPosition = (float)(_startingScreen - 1) / (float)(_screens - 1); + + _containerSize = (int)_screensContainer.gameObject.GetComponent().offsetMax.y; + + ChangeBulletsInfo(CurrentScreen()); + + if (NextButton) + NextButton.GetComponent