///Credit perchik
///Sourced from - http://forum.unity3d.com/threads/receive-onclick-event-and-pass-it-on-to-lower-ui-elements.293642/
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions
{
///
/// Extension to the UI class which creates a dropdown list
///
[RequireComponent(typeof(RectTransform))]
[AddComponentMenu("UI/Extensions/ComboBox/Dropdown List")]
public class DropDownList : MonoBehaviour
{
public Color disabledTextColor;
public DropDownListItem SelectedItem { get; private set; } //outside world gets to get this, not set it
public List Items;
public bool OverrideHighlighted = true;
//private bool isInitialized = false;
private bool _isPanelActive = false;
private bool _hasDrawnOnce = false;
private DropDownListButton _mainButton;
private RectTransform _rectTransform;
private RectTransform _overlayRT;
private RectTransform _scrollPanelRT;
private RectTransform _scrollBarRT;
private RectTransform _slidingAreaRT;
private RectTransform _scrollHandleRT;
private RectTransform _itemsPanelRT;
private Canvas _canvas;
private RectTransform _canvasRT;
private ScrollRect _scrollRect;
private List _panelItems;
private GameObject _itemTemplate;
[SerializeField]
private float dropdownOffset;
[SerializeField]
private float _scrollBarWidth = 20.0f;
public float ScrollBarWidth
{
get { return _scrollBarWidth; }
set
{
_scrollBarWidth = value;
RedrawPanel();
}
}
// private int scrollOffset; //offset of the selected item
private int _selectedIndex = -1;
[SerializeField]
private int _itemsToDisplay;
public int ItemsToDisplay
{
get { return _itemsToDisplay; }
set
{
_itemsToDisplay = value;
RedrawPanel();
}
}
public bool SelectFirstItemOnStart = false;
[SerializeField]
private bool _displayPanelAbove = false;
[System.Serializable]
public class SelectionChangedEvent : Events.UnityEvent { }
// fires when item is changed;
public SelectionChangedEvent OnSelectionChanged;
public void Start()
{
Initialize();
if (SelectFirstItemOnStart && Items.Count > 0) {
ToggleDropdownPanel (false);
OnItemClicked (0);
}
RedrawPanel();
}
private bool Initialize()
{
bool success = true;
try
{
_rectTransform = GetComponent();
_mainButton = new DropDownListButton(_rectTransform.Find("MainButton").gameObject);
_overlayRT = _rectTransform.Find("Overlay").GetComponent();
_overlayRT.gameObject.SetActive(false);
_scrollPanelRT = _overlayRT.Find("ScrollPanel").GetComponent();
_scrollBarRT = _scrollPanelRT.Find("Scrollbar").GetComponent();
_slidingAreaRT = _scrollBarRT.Find("SlidingArea").GetComponent();
_scrollHandleRT = _slidingAreaRT.Find("Handle").GetComponent();
_itemsPanelRT = _scrollPanelRT.Find("Items").GetComponent();
//itemPanelLayout = itemsPanelRT.gameObject.GetComponent();
_canvas = GetComponentInParent