From 4b1c5412f166e7a024a4c9a0cc5fa081191baab3 Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Sun, 1 Jan 2023 15:57:31 +0000 Subject: [PATCH] Updated UIVerticalScroller for standards and added UIHorizontalScroller Resolves: https://github.com/Unity-UI-Extensions/com.unity.uiextensions/issues/205 --- Editor/UIExtensionsMenuOptions.cs | 89 +++++- .../Scripts/Layout/UIHorizontalScroller.cs | 276 ++++++++++++++++++ .../Layout/UIHorizontalScroller.cs.meta | 11 + Runtime/Scripts/Layout/UIVerticalScroller.cs | 237 ++++++++------- 4 files changed, 497 insertions(+), 116 deletions(-) create mode 100644 Runtime/Scripts/Layout/UIHorizontalScroller.cs create mode 100644 Runtime/Scripts/Layout/UIHorizontalScroller.cs.meta diff --git a/Editor/UIExtensionsMenuOptions.cs b/Editor/UIExtensionsMenuOptions.cs index 01c7770..75711d6 100644 --- a/Editor/UIExtensionsMenuOptions.cs +++ b/Editor/UIExtensionsMenuOptions.cs @@ -620,7 +620,7 @@ namespace UnityEditor.UI rectTransformCenter.anchorMax = new Vector2(1f, 0.6f); rectTransformCenter.sizeDelta = Vector2.zero; - uiscr.center = uiScrollerCenter.GetComponent(); + uiscr.Center = uiScrollerCenter.GetComponent(); //Setup Content container RectTransform rectTransformContent = childContent.GetComponent(); @@ -669,10 +669,91 @@ namespace UnityEditor.UI Selection.activeGameObject = uiVerticalScrollerRoot; } - #endregion + #endregion - #region UI Button - [MenuItem("GameObject/UI/Extensions/Controls/UI Button", false)] + #region UIHorizontal Scroller + [MenuItem("GameObject/UI/Extensions/Layout/UI Horizontal Scroller", false)] + static public void AddUIHorizontalScroller(MenuCommand menuCommand) + { + GameObject uiHorizontalScrollerRoot = CreateUIElementRoot("UI Horizontal Scroller", menuCommand, s_ThickGUIElementSize); + + GameObject uiScrollerCenter = CreateUIObject("Center", uiHorizontalScrollerRoot); + + GameObject childContent = CreateUIObject("Content", uiHorizontalScrollerRoot); + + // Set RectTransform to stretch + RectTransform rectTransformScrollSnapRoot = uiHorizontalScrollerRoot.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(500f, 150f); + + // Add required ScrollRect + ScrollRect sr = uiHorizontalScrollerRoot.AddComponent(); + sr.vertical = false; + sr.horizontal = true; + sr.movementType = ScrollRect.MovementType.Unrestricted; + var uiscr = uiHorizontalScrollerRoot.AddComponent(); + + //Setup container center point + RectTransform rectTransformCenter = uiScrollerCenter.GetComponent(); + rectTransformCenter.anchorMin = new Vector2(0.3f,0f); + rectTransformCenter.anchorMax = new Vector2(0.6f,1f); + rectTransformCenter.sizeDelta = Vector2.zero; + + uiscr.Center = uiScrollerCenter.GetComponent(); + + //Setup Content container + RectTransform rectTransformContent = childContent.GetComponent(); + rectTransformContent.anchorMin = Vector2.zero; + rectTransformContent.anchorMax = new Vector2(1f, 1f); + rectTransformContent.sizeDelta = Vector2.zero; + + sr.content = rectTransformContent; + + // Add sample children + for (int i = 0; i < 10; i++) + { + GameObject childPage = CreateUIObject("Page_" + i, childContent); + + GameObject childText = CreateUIObject("Text", childPage); + + //Setup 1st Child + Image pageImage = childPage.AddComponent(); + pageImage.sprite = AssetDatabase.GetBuiltinExtraResource(kStandardSpritePath); + pageImage.type = Image.Type.Sliced; + pageImage.color = s_DefaultSelectableColor; + + RectTransform rectTransformPage = childPage.GetComponent(); + rectTransformPage.anchorMin = new Vector2(0.5f, 0); + rectTransformPage.anchorMax = new Vector2(0.5f, 1f); + rectTransformPage.sizeDelta = new Vector2(80f, 0f); + rectTransformPage.pivot = new Vector2(0.5f, 0.5f); + rectTransformPage.localPosition = new Vector3(80 * i, 0, 0); + childPage.AddComponent