Skip to content

Instantly share code, notes, and snippets.

@mandarinx
Forked from QubitsDev/ScrollRectAutoScroll.cs
Created August 22, 2017 11:30
Show Gist options
  • Save mandarinx/eae10c9e8d1a5534b7b19b74aeb2a665 to your computer and use it in GitHub Desktop.
Save mandarinx/eae10c9e8d1a5534b7b19b74aeb2a665 to your computer and use it in GitHub Desktop.
Unity3d ScrollRect Auto-Scroll, Dropdown Use: Places this component in the Template of Dropdown (with the ScrollRect component)
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(ScrollRect))]
public class ScrollRectAutoScroll : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public float scrollSpeed = 10f;
private bool mouseOver = false;
private List<Selectable> m_Selectables = new List<Selectable>();
private ScrollRect m_ScrollRect;
private Vector2 m_NextScrollPosition = Vector2.up;
void OnEnable()
{
if (m_ScrollRect)
{
m_ScrollRect.content.GetComponentsInChildren(m_Selectables);
}
}
void Awake()
{
m_ScrollRect = GetComponent<ScrollRect>();
}
void Start()
{
if (m_ScrollRect)
{
m_ScrollRect.content.GetComponentsInChildren(m_Selectables);
}
ScrollToSelected(true);
}
void Update()
{
// Scroll via input.
InputScroll();
if (!mouseOver)
{
// Lerp scrolling code.
m_ScrollRect.normalizedPosition = Vector2.Lerp(m_ScrollRect.normalizedPosition, m_NextScrollPosition, scrollSpeed * Time.deltaTime);
}
else
{
m_NextScrollPosition = m_ScrollRect.normalizedPosition;
}
}
void InputScroll()
{
if (m_Selectables.Count > 0)
{
if (Input.GetButtonDown("Horizontal") || Input.GetButtonDown("Vertical") || Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
ScrollToSelected(false);
}
}
}
void ScrollToSelected(bool quickScroll)
{
int selectedIndex = -1;
Selectable selectedElement = EventSystem.current.currentSelectedGameObject ? EventSystem.current.currentSelectedGameObject.GetComponent<Selectable>() : null;
if (selectedElement)
{
selectedIndex = m_Selectables.IndexOf(selectedElement);
}
if (selectedIndex > -1)
{
if (quickScroll)
{
m_ScrollRect.normalizedPosition = new Vector2(0, 1 - (selectedIndex / ((float)m_Selectables.Count - 1)));
m_NextScrollPosition = m_ScrollRect.normalizedPosition;
}
else
{
m_NextScrollPosition = new Vector2(0, 1 - (selectedIndex / ((float)m_Selectables.Count - 1)));
}
}
}
public void OnPointerEnter(PointerEventData eventData)
{
mouseOver = true;
}
public void OnPointerExit(PointerEventData eventData)
{
mouseOver = false;
ScrollToSelected(false);
}
}
@j-schoch
Copy link

j-schoch commented Sep 27, 2024

I stripped this down to instantly scroll to the selected item when the dropdown opens and nothing else.

Figured I'd share it and maybe save someone some time:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(ScrollRect))]
public class ScrollRectAutoScroll : MonoBehaviour
{
    private readonly List<Selectable> _selectables = new();
    private ScrollRect _scrollRect;

    private void Awake()
    {
        _scrollRect = GetComponent<ScrollRect>();
    }

    private void Start()
    {
        ScrollToSelected();
    }

    private void ScrollToSelected()
    {
        if (_scrollRect == null) { return; }

        _scrollRect.content.GetComponentsInChildren(_selectables);

        if(_selectables.Count <= 1) { return; }

        GameObject selected = EventSystem.current.currentSelectedGameObject;
        if (selected != null && selected.TryGetComponent(out Selectable selectedElement))
        {
            int selectedIndex = _selectables.IndexOf(selectedElement);
            float normalizedPositionY = 1 - Mathf.InverseLerp(0, _selectables.Count - 1, selectedIndex);
            _scrollRect.normalizedPosition = Vector2.up * normalizedPositionY;
        }
    }
}

@Roombie
Copy link

Roombie commented Dec 1, 2024

Thank you so much!

@rtritean
Copy link

@j-schoch works great for mouse and kb with the new input system, but when scrolling the list using a gamepad, the list is not being scrolled. Any idea how to fix this?

@JeffersonRodrigues7
Copy link

god, thanks!!!!

@lyraignifan
Copy link

This script is so so useful! Thank you so much for making it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment