///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; using System.Linq; namespace UnityEngine.UI.Extensions { [RequireComponent(typeof(RectTransform))] [AddComponentMenu("UI/Extensions/ComboBox")] public class ComboBox : MonoBehaviour { public Color disabledTextColor; public DropDownListItem SelectedItem { get; private set; } //outside world gets to get this, not set it public List AvailableOptions; [SerializeField] private float _scrollBarWidth = 20.0f; [SerializeField] private int _itemsToDisplay; //Sorting disabled as it causes issues. //[SerializeField] //private bool _sortItems = true; [SerializeField] private bool _displayPanelAbove = false; [System.Serializable] public class SelectionChangedEvent : UnityEngine.Events.UnityEvent { } // fires when item is changed; public SelectionChangedEvent OnSelectionChanged; //private bool isInitialized = false; private bool _isPanelActive = false; private bool _hasDrawnOnce = false; private InputField _mainInput; private RectTransform _inputRT; 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; //items that will get shown in the drop-down private Dictionary panelObjects; private GameObject itemTemplate; public string Text { get; private set; } public float ScrollBarWidth { get { return _scrollBarWidth; } set { _scrollBarWidth = value; RedrawPanel(); } } // private int scrollOffset; //offset of the selected item // private int _selectedIndex = 0; public int ItemsToDisplay { get { return _itemsToDisplay; } set { _itemsToDisplay = value; RedrawPanel(); } } public void Awake() { Initialize(); } public void Start() { RedrawPanel(); } private bool Initialize() { bool success = true; try { _rectTransform = GetComponent(); _inputRT = _rectTransform.Find("InputField").GetComponent(); _mainInput = _inputRT.GetComponent(); _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(); _canvasRT = _canvas.GetComponent(); _scrollRect = _scrollPanelRT.GetComponent(); _scrollRect.scrollSensitivity = _rectTransform.sizeDelta.y / 2; _scrollRect.movementType = ScrollRect.MovementType.Clamped; _scrollRect.content = _itemsPanelRT; itemTemplate = _rectTransform.Find("ItemTemplate").gameObject; itemTemplate.SetActive(false); } catch (System.NullReferenceException ex) { Debug.LogException(ex); Debug.LogError("Something is setup incorrectly with the dropdownlist component causing a Null Reference Exception"); success = false; } panelObjects = new Dictionary(); _panelItems = AvailableOptions.ToList(); RebuildPanel(); //RedrawPanel(); - causes an initialisation failure in U5 return success; } public void AddItem(string item) { AvailableOptions.Add(item); RebuildPanel(); } public void RemoveItem(string item) { AvailableOptions.Remove(item); RebuildPanel(); } public void SetAvailableOptions(List newOptions) { AvailableOptions.Clear(); AvailableOptions = newOptions; RebuildPanel(); } public void SetAvailableOptions(string[] newOptions) { AvailableOptions.Clear(); for (int i = 0; i < newOptions.Length; i++) { AvailableOptions.Add(newOptions[i]); } RebuildPanel(); } public void ResetItems() { AvailableOptions.Clear(); RebuildPanel(); } /// /// Rebuilds the contents of the panel in response to items being added. /// private void RebuildPanel() { //panel starts with all options _panelItems.Clear(); foreach (string option in AvailableOptions) { _panelItems.Add(option.ToLower()); } //if(_sortItems) _panelItems.Sort(); List itemObjs = new List(panelObjects.Values); panelObjects.Clear(); int indx = 0; while (itemObjs.Count < AvailableOptions.Count) { GameObject newItem = Instantiate(itemTemplate) as GameObject; newItem.name = "Item " + indx; newItem.transform.SetParent(_itemsPanelRT, false); itemObjs.Add(newItem); indx++; } for (int i = 0; i < itemObjs.Count; i++) { itemObjs[i].SetActive(i <= AvailableOptions.Count); if (i < AvailableOptions.Count) { itemObjs[i].name = "Item " + i + " " + _panelItems[i]; itemObjs[i].transform.Find("Text").GetComponent().text = AvailableOptions[i]; //set the text value Button itemBtn = itemObjs[i].GetComponent