/// Credit Mrs. YakaYocha /// Sourced from - https://www.youtube.com/channel/UCHp8LZ_0-iCvl-5pjHATsgw /// Please donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RJ8D9FRFQF9VS using UnityEngine.Events; namespace UnityEngine.UI.Extensions { [RequireComponent(typeof(ScrollRect))] [AddComponentMenu("Layout/Extensions/Vertical Scroller")] public class UIVerticalScroller : MonoBehaviour { [Tooltip("Scrollable area (content of desired ScrollRect)")] public RectTransform _scrollingPanel; [Tooltip("Elements to populate inside the scroller")] public GameObject[] _arrayOfElements; [Tooltip("Center display area (position of zoomed content)")] public RectTransform _center; [Tooltip("Select the item to be in center on start. (optional)")] public int StartingIndex = -1; [Tooltip("Button to go to the next page. (optional)")] public GameObject ScrollUpButton; [Tooltip("Button to go to the previous page. (optional)")] public GameObject ScrollDownButton; [Tooltip("Event fired when a specific item is clicked, exposes index number of item. (optional)")] public UnityEvent ButtonClicked; private float[] distReposition; private float[] distance; //private int elementsDistance; private int minElementsNum; private int elementLength; //private int elementHalfLength; private float deltaY; private string result; public UIVerticalScroller() { } public UIVerticalScroller(RectTransform scrollingPanel, GameObject[] arrayOfElements, RectTransform center) { _scrollingPanel = scrollingPanel; _arrayOfElements = arrayOfElements; _center = center; } public void Awake() { var scrollRect = GetComponent(); if (!_scrollingPanel) { _scrollingPanel = scrollRect.content; } if (!_center) { Debug.LogError("Please define the RectTransform for the Center viewport of the scrollable area"); } 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 (_arrayOfElements.Length < 1) { Debug.Log("No child content found, exiting.."); return; } elementLength = _arrayOfElements.Length; distance = new float[elementLength]; distReposition = new float[elementLength]; //get distance between buttons //elementsDistance = (int)Mathf.Abs(_arrayOfElements[1].GetComponent().anchoredPosition.y - _arrayOfElements[0].GetComponent().anchoredPosition.y); deltaY = _arrayOfElements[0].GetComponent().rect.height * elementLength / 3 * 2; Vector2 startPosition = new Vector2(_scrollingPanel.anchoredPosition.x, -deltaY); _scrollingPanel.anchoredPosition = startPosition; for (var i = 0; i < _arrayOfElements.Length; i++) { AddListener(_arrayOfElements[i], i); } if (ScrollUpButton) ScrollUpButton.GetComponent