Created
November 24, 2023 13:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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