///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/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 _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;
[System.Serializable]
public class SelectionChangedEvent : UnityEngine.Events.UnityEvent {
}
// fires when item is changed;
public SelectionChangedEvent OnSelectionChanged;
public void Start()
{
Initialize();
if (SelectFirstItemOnStart && Items.Count > 0) {
ToggleDropdownPanel (false);
OnItemClicked (0);
}
}
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.FindChild("Handle").GetComponent();
_itemsPanelRT = _scrollPanelRT.Find("Items").GetComponent();
//itemPanelLayout = itemsPanelRT.gameObject.GetComponent();
_canvas = GetComponentInParent