/// Credit Ahmad S. Al-Faqeeh /// Sourced from - https://github.com/Unity-UI-Extensions/com.unity.uiextensions/issues/205 /// Based on the UIVerticalScroller /// using UnityEngine.Events; namespace UnityEngine.UI.Extensions { [RequireComponent(typeof(ScrollRect))] [AddComponentMenu("Layout/Extensions/Horizontal Scroller")] public class UIHorizontalScroller : MonoBehaviour { private float[] distReposition; private float[] distance; [SerializeField] [Tooltip("desired ScrollRect")] private ScrollRect scrollRect; [SerializeField] [Tooltip("Elements to populate inside the scroller")] private GameObject[] arrayOfElements; [SerializeField] [Tooltip("Center display area (position of zoomed content)")] private RectTransform center; [SerializeField] [Tooltip("Size / spacing of elements")] private RectTransform elementSize; [SerializeField] [Tooltip("Scale = 1/ (1+distance from center * shrinkage)")] private Vector2 elementShrinkage = new Vector2(1f / 200, 1f / 200); [SerializeField] [Tooltip("Minimum element scale (furthest from center)")] private Vector2 minScale = new Vector2(0.7f, 0.7f); [SerializeField] [Tooltip("Select the item to be in center on start. (optional)")] private int startingIndex = -1; [SerializeField] [Tooltip("Stop scrolling past last element from inertia.")] private bool stopMomentumOnEnd = true; [SerializeField] [Tooltip("Set Items out of center to not interactible.")] private bool disableUnfocused = true; [SerializeField] [Tooltip("Button to go to the next page. (optional)")] private GameObject scrollLeftButton; [SerializeField] [Tooltip("Button to go to the previous page. (optional)")] private GameObject scrollRightButton; [SerializeField] [Tooltip("Event fired when a specific item is clicked, exposes index number of item. (optional)")] private UnityEvent onButtonClicked; [SerializeField] [Tooltip("Event fired when the focused item is Changed. (optional)")] private UnityEvent onFocusChanged; public int FocusedElementIndex { get; private set; } public RectTransform Center { get => center; set => center = value; } //Scrollable area (content of desired ScrollRect) public RectTransform ScrollingPanel { get { return scrollRect.content; } } public string Result { get; private set; } public UIHorizontalScroller() { } public UIHorizontalScroller(RectTransform center, RectTransform elementSize, ScrollRect scrollRect, GameObject[] arrayOfElements) { this.scrollRect = scrollRect; this.elementSize = elementSize; this.arrayOfElements = arrayOfElements; this.center = center; } public void Awake() { if (!scrollRect) { scrollRect = GetComponent(); } if (!center) { Debug.LogError("Please define the RectTransform for the Center viewport of the scrollable area"); } if (!elementSize) { elementSize = center; } if (arrayOfElements == null || arrayOfElements.Length == 0) { var childCount = scrollRect.content.childCount; if (childCount > 0) { arrayOfElements = new GameObject[childCount]; for (int i = 0; i < childCount; i++) { arrayOfElements[i] = scrollRect.content.GetChild(i).gameObject; } } } } public void Start() { if (scrollLeftButton) { scrollLeftButton.GetComponent