Skip to content

Instantly share code, notes, and snippets.

@bkevelham
Created November 24, 2023 13:52
Show Gist options
  • Save bkevelham/f585207c7f013b2fbefb1a03a707bbed to your computer and use it in GitHub Desktop.
Save bkevelham/f585207c7f013b2fbefb1a03a707bbed to your computer and use it in GitHub Desktop.
A small component which ensures that a Unity Dropdown, after having a value selected, will show that value at the top when reopening the dropdown.
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DropdownFixer : MonoBehaviour, IPointerClickHandler
{
public Dropdown Dropdown;
public int ElementHeight = 50; //The height of the elements in your Dropdown
public void OnPointerClick(PointerEventData eventData)
{
RectTransform[] transforms = GetComponentsInChildren<RectTransform>();
RectTransform content = null;
foreach(RectTransform t in transforms)
{
if(t.name == "Content")
{
content = t;
break;
}
}
if (content == null) return;
int index = Dropdown.value;
content.localPosition = content.localPosition + Vector3.up * index * ElementHeight;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment