The unity dropdown that I’ve made in my scene doesn’t scroll to the selected object when there are more options than the screen can handle.
It happens when I try to navigate with the arrow keys through the dropdown list.
The unity dropdown that I’ve made in my scene doesn’t scroll to the selected object when there are more options than the screen can handle.
It happens when I try to navigate with the arrow keys through the dropdown list.
Here’s a script I just made for dropdown menus in my project. You won’t be able to use it without some modifications as I’ve coded it to work with some Rewired inputs that are specific to my project. I also put the script on the “Template” child game object of the Dropdown menu object. It’s not perfect and it only supports vertical auto scrolling but it gets the job done in my case.
using System.Collections.Generic;
using Rewired;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ScrollRectAutoScroll : MonoBehaviour {
List<Selectable> selectables = new List<Selectable>();
int elementCount;
#region Properties
Player RewiredPlayer { get; set; }
ScrollRect ScrollRectComponent { get; set; }
#endregion
void OnEnable() {
if(ScrollRectComponent) {
ScrollRectComponent.content.GetComponentsInChildren(selectables);
elementCount = selectables.Count;
}
}
void Awake() {
RewiredPlayer = ReInput.players.GetPlayer(0);
ScrollRectComponent = GetComponent<ScrollRect>();
}
void Start() {
ScrollRectComponent.content.GetComponentsInChildren(selectables);
elementCount = selectables.Count;
}
void Update() {
if(elementCount > 0)
if(RewiredPlayer.GetButtonDown("Menu Horizontal") || RewiredPlayer.GetNegativeButtonDown("Menu Horizontal") || RewiredPlayer.GetButtonDown("Menu Vertical") || RewiredPlayer.GetNegativeButtonDown("Menu Vertical")) {
int selectedIndex = -1;
Selectable selectedElement = EventSystem.current.currentSelectedGameObject ? EventSystem.current.currentSelectedGameObject.GetComponent<Selectable>() : null;
if(selectedElement)
selectedIndex = selectables.IndexOf(selectedElement);
if(selectedIndex > -1)
ScrollRectComponent.verticalNormalizedPosition = 1 - (selectedIndex / ((float)elementCount - 1));
}
}
}
This script did the trick for me
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[RequireComponent(typeof(ScrollRect))]
public class ScrollRectEnsureVisible : MonoBehaviour
{
RectTransform scrollRectTransform;
RectTransform contentPanel;
RectTransform selectedRectTransform;
GameObject lastSelected;
void Start()
{
scrollRectTransform = GetComponent<RectTransform>();
}
void Update()
{
//just incase content panel gets created in start.
if(contentPanel == null) contentPanel = GetComponent<ScrollRect>().content;
GameObject selected = EventSystem.current.currentSelectedGameObject;
if (selected == null)
{
return;
}
if (selected.transform.parent != contentPanel.transform)
{
return;
}
if (selected == lastSelected)
{
return;
}
selectedRectTransform = selected.GetComponent<RectTransform>();
contentPanel.anchoredPosition = new Vector2(contentPanel.anchoredPosition.x, - (selectedRectTransform.localPosition.y) - (selectedRectTransform.rect.height/2));
lastSelected = selected;
}
}