2015-02-10 08:03:38 +08:00
|
|
|
|
///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
|
|
|
|
|
{
|
2017-05-05 21:25:48 +08:00
|
|
|
|
/// <summary>
|
2023-08-16 18:02:41 +08:00
|
|
|
|
/// Extension to the UI class which creates a dropdown list
|
2017-05-05 21:25:48 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
[RequireComponent(typeof(RectTransform))]
|
2023-02-07 22:35:43 +08:00
|
|
|
|
[AddComponentMenu("UI/Extensions/ComboBox/Dropdown List")]
|
2015-02-10 08:03:38 +08:00
|
|
|
|
public class DropDownList : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public Color disabledTextColor;
|
|
|
|
|
public DropDownListItem SelectedItem { get; private set; } //outside world gets to get this, not set it
|
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
[Header("Dropdown List Items")]
|
|
|
|
|
public List<DropDownListItem> Items;
|
|
|
|
|
|
|
|
|
|
[Header("Properties")]
|
|
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private bool isActive = true;
|
|
|
|
|
|
2016-07-15 01:48:18 +08:00
|
|
|
|
public bool OverrideHighlighted = true;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
|
|
|
|
//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;
|
2021-05-11 01:38:31 +08:00
|
|
|
|
private RectTransform _scrollHandleRT;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
private RectTransform _itemsPanelRT;
|
|
|
|
|
private Canvas _canvas;
|
|
|
|
|
private RectTransform _canvasRT;
|
|
|
|
|
|
|
|
|
|
private ScrollRect _scrollRect;
|
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
private List<DropDownListButton> _panelItems = new List<DropDownListButton>();
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
private GameObject _itemTemplate;
|
|
|
|
|
private bool _initialized;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
2023-08-16 18:02:41 +08:00
|
|
|
|
private string _defaultMainButtonCaption = null;
|
|
|
|
|
private Color _defaultNormalColor;
|
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
[SerializeField]
|
2015-02-10 08:03:38 +08:00
|
|
|
|
private float _scrollBarWidth = 20.0f;
|
|
|
|
|
public float ScrollBarWidth
|
|
|
|
|
{
|
|
|
|
|
get { return _scrollBarWidth; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_scrollBarWidth = value;
|
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// private int scrollOffset; //offset of the selected item
|
2017-05-17 01:51:24 +08:00
|
|
|
|
private int _selectedIndex = -1;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private int _itemsToDisplay;
|
|
|
|
|
public int ItemsToDisplay
|
|
|
|
|
{
|
|
|
|
|
get { return _itemsToDisplay; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_itemsToDisplay = value;
|
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private float dropdownOffset;
|
2017-03-27 18:52:51 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private bool _displayPanelAbove = false;
|
2021-05-11 01:38:31 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
public bool SelectFirstItemOnStart = false;
|
2017-03-27 20:33:08 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private int selectItemIndexOnStart = 0;
|
|
|
|
|
private bool shouldSelectItemOnStart => SelectFirstItemOnStart || selectItemIndexOnStart > 0;
|
2017-03-27 20:33:08 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
[System.Serializable]
|
|
|
|
|
public class SelectionChangedEvent : Events.UnityEvent<int> { }
|
|
|
|
|
|
|
|
|
|
// fires when item is changed;
|
|
|
|
|
[Header("Events")]
|
|
|
|
|
public SelectionChangedEvent OnSelectionChanged;
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public class ControlDisabledEvent : Events.UnityEvent<bool> { }
|
|
|
|
|
|
|
|
|
|
// fires when item is changed;
|
|
|
|
|
public ControlDisabledEvent OnControlDisabled;
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
Initialize();
|
|
|
|
|
if (shouldSelectItemOnStart && Items.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
SelectItemIndex(SelectFirstItemOnStart ? 0 : selectItemIndexOnStart);
|
|
|
|
|
}
|
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
private bool Initialize()
|
2015-02-10 08:03:38 +08:00
|
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
|
if (_initialized) return true;
|
|
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
|
bool success = true;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_rectTransform = GetComponent<RectTransform>();
|
2017-06-04 00:59:12 +08:00
|
|
|
|
_mainButton = new DropDownListButton(_rectTransform.Find("MainButton").gameObject);
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
2023-08-16 18:02:41 +08:00
|
|
|
|
_defaultMainButtonCaption = _mainButton.txt.text;
|
|
|
|
|
_defaultNormalColor = _mainButton.btn.colors.normalColor;
|
|
|
|
|
|
2017-06-04 00:59:12 +08:00
|
|
|
|
_overlayRT = _rectTransform.Find("Overlay").GetComponent<RectTransform>();
|
2015-02-10 08:03:38 +08:00
|
|
|
|
_overlayRT.gameObject.SetActive(false);
|
2017-06-04 00:59:12 +08:00
|
|
|
|
_scrollPanelRT = _overlayRT.Find("ScrollPanel").GetComponent<RectTransform>();
|
|
|
|
|
_scrollBarRT = _scrollPanelRT.Find("Scrollbar").GetComponent<RectTransform>();
|
|
|
|
|
_slidingAreaRT = _scrollBarRT.Find("SlidingArea").GetComponent<RectTransform>();
|
2021-05-11 01:38:31 +08:00
|
|
|
|
_scrollHandleRT = _slidingAreaRT.Find("Handle").GetComponent<RectTransform>();
|
2017-06-04 00:59:12 +08:00
|
|
|
|
_itemsPanelRT = _scrollPanelRT.Find("Items").GetComponent<RectTransform>();
|
2015-02-10 08:03:38 +08:00
|
|
|
|
//itemPanelLayout = itemsPanelRT.gameObject.GetComponent<LayoutGroup>();
|
|
|
|
|
|
|
|
|
|
_canvas = GetComponentInParent<Canvas>();
|
|
|
|
|
_canvasRT = _canvas.GetComponent<RectTransform>();
|
|
|
|
|
|
|
|
|
|
_scrollRect = _scrollPanelRT.GetComponent<ScrollRect>();
|
|
|
|
|
_scrollRect.scrollSensitivity = _rectTransform.sizeDelta.y / 2;
|
|
|
|
|
_scrollRect.movementType = ScrollRect.MovementType.Clamped;
|
|
|
|
|
_scrollRect.content = _itemsPanelRT;
|
|
|
|
|
|
2017-06-04 00:59:12 +08:00
|
|
|
|
_itemTemplate = _rectTransform.Find("ItemTemplate").gameObject;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
_itemTemplate.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
catch (System.NullReferenceException ex)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogException(ex);
|
2016-07-15 01:48:18 +08:00
|
|
|
|
Debug.LogError("Something is setup incorrectly with the dropdownlist component causing a Null Reference Exception");
|
2015-02-10 08:03:38 +08:00
|
|
|
|
success = false;
|
|
|
|
|
}
|
2023-02-07 22:35:43 +08:00
|
|
|
|
_initialized = true;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
RebuildPanel();
|
2015-02-10 08:03:38 +08:00
|
|
|
|
RedrawPanel();
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update the drop down selection to a specific index
|
2020-09-24 22:36:02 +08:00
|
|
|
|
/// </summary>
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// <param name="index"></param>
|
|
|
|
|
public void SelectItemIndex(int index)
|
|
|
|
|
{
|
|
|
|
|
ToggleDropdownPanel(false);
|
|
|
|
|
OnItemClicked(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// currently just using items in the list instead of being able to add to it.
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Rebuilds the list from a new collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// NOTE, this will clear all existing items
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="list"></param>
|
|
|
|
|
public void RefreshItems(params object[] list)
|
2015-02-10 08:03:38 +08:00
|
|
|
|
{
|
2020-09-24 22:36:02 +08:00
|
|
|
|
Items.Clear();
|
2015-02-10 08:03:38 +08:00
|
|
|
|
List<DropDownListItem> ddItems = new List<DropDownListItem>();
|
|
|
|
|
foreach (var obj in list)
|
|
|
|
|
{
|
|
|
|
|
if (obj is DropDownListItem)
|
|
|
|
|
{
|
|
|
|
|
ddItems.Add((DropDownListItem)obj);
|
|
|
|
|
}
|
|
|
|
|
else if (obj is string)
|
|
|
|
|
{
|
|
|
|
|
ddItems.Add(new DropDownListItem(caption: (string)obj));
|
|
|
|
|
}
|
|
|
|
|
else if (obj is Sprite)
|
|
|
|
|
{
|
|
|
|
|
ddItems.Add(new DropDownListItem(image: (Sprite)obj));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new System.Exception("Only ComboBoxItems, Strings, and Sprite types are allowed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Items.AddRange(ddItems);
|
|
|
|
|
RebuildPanel();
|
2023-02-07 22:35:43 +08:00
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
2020-09-24 22:36:02 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds an additional item to the drop down list (recommended)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">Item of type DropDownListItem</param>
|
|
|
|
|
public void AddItem(DropDownListItem item)
|
2020-09-24 22:36:02 +08:00
|
|
|
|
{
|
|
|
|
|
Items.Add(item);
|
|
|
|
|
RebuildPanel();
|
2023-02-07 22:35:43 +08:00
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
2020-09-24 22:36:02 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// <summary>
|
2023-08-16 18:02:41 +08:00
|
|
|
|
/// Adds an additional drop down list item using a string name
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">Item of type String</param>
|
|
|
|
|
public void AddItem(string item)
|
2020-09-24 22:36:02 +08:00
|
|
|
|
{
|
|
|
|
|
Items.Add(new DropDownListItem(caption: (string)item));
|
|
|
|
|
RebuildPanel();
|
2023-02-07 22:35:43 +08:00
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
2020-09-24 22:36:02 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// <summary>
|
2023-08-16 18:02:41 +08:00
|
|
|
|
/// Adds an additional drop down list item using a sprite image
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">Item of type UI Sprite</param>
|
|
|
|
|
public void AddItem(Sprite item)
|
2020-09-24 22:36:02 +08:00
|
|
|
|
{
|
|
|
|
|
Items.Add(new DropDownListItem(image: (Sprite)item));
|
|
|
|
|
RebuildPanel();
|
2023-02-07 22:35:43 +08:00
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
2020-09-24 22:36:02 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes an item from the drop down list (recommended)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">Item of type DropDownListItem</param>
|
|
|
|
|
public void RemoveItem(DropDownListItem item)
|
2020-09-24 22:36:02 +08:00
|
|
|
|
{
|
|
|
|
|
Items.Remove(item);
|
|
|
|
|
RebuildPanel();
|
2023-02-07 22:35:43 +08:00
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
2020-09-24 22:36:02 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// <summary>
|
2023-08-16 18:02:41 +08:00
|
|
|
|
/// Removes an item from the drop down list item using a string name
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">Item of type String</param>
|
|
|
|
|
public void RemoveItem(string item)
|
2020-09-24 22:36:02 +08:00
|
|
|
|
{
|
|
|
|
|
Items.Remove(new DropDownListItem(caption: (string)item));
|
|
|
|
|
RebuildPanel();
|
2023-02-07 22:35:43 +08:00
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
2020-09-24 22:36:02 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// <summary>
|
2023-08-16 18:02:41 +08:00
|
|
|
|
/// Removes an item from the drop down list item using a sprite image
|
2023-02-07 22:35:43 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">Item of type UI Sprite</param>
|
|
|
|
|
public void RemoveItem(Sprite item)
|
2020-09-24 22:36:02 +08:00
|
|
|
|
{
|
|
|
|
|
Items.Remove(new DropDownListItem(image: (Sprite)item));
|
|
|
|
|
RebuildPanel();
|
2023-02-07 22:35:43 +08:00
|
|
|
|
RedrawPanel();
|
|
|
|
|
}
|
2020-09-24 22:36:02 +08:00
|
|
|
|
|
2023-08-16 18:02:41 +08:00
|
|
|
|
public void ResetDropDown()
|
|
|
|
|
{
|
|
|
|
|
if (!_initialized)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_mainButton.txt.text = _defaultMainButtonCaption;
|
|
|
|
|
for (int i = 0; i < _itemsPanelRT.childCount; i++)
|
|
|
|
|
_panelItems[i].btnImg.color = _defaultNormalColor;
|
|
|
|
|
|
|
|
|
|
_selectedIndex = -1;
|
|
|
|
|
_initialized = false;
|
|
|
|
|
Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetItems()
|
2020-09-24 22:36:02 +08:00
|
|
|
|
{
|
|
|
|
|
Items.Clear();
|
|
|
|
|
RebuildPanel();
|
2023-02-07 22:35:43 +08:00
|
|
|
|
RedrawPanel();
|
2020-09-24 22:36:02 +08:00
|
|
|
|
}
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Rebuilds the contents of the panel in response to items being added.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void RebuildPanel()
|
|
|
|
|
{
|
|
|
|
|
if (Items.Count == 0) return;
|
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
if (!_initialized)
|
|
|
|
|
{
|
|
|
|
|
Start();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-10 08:03:38 +08:00
|
|
|
|
int indx = _panelItems.Count;
|
|
|
|
|
while (_panelItems.Count < Items.Count)
|
|
|
|
|
{
|
|
|
|
|
GameObject newItem = Instantiate(_itemTemplate) as GameObject;
|
|
|
|
|
newItem.name = "Item " + indx;
|
|
|
|
|
newItem.transform.SetParent(_itemsPanelRT, false);
|
|
|
|
|
|
|
|
|
|
_panelItems.Add(new DropDownListButton(newItem));
|
|
|
|
|
indx++;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < _panelItems.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i < Items.Count)
|
|
|
|
|
{
|
|
|
|
|
DropDownListItem item = Items[i];
|
|
|
|
|
|
|
|
|
|
_panelItems[i].txt.text = item.Caption;
|
|
|
|
|
if (item.IsDisabled) _panelItems[i].txt.color = disabledTextColor;
|
|
|
|
|
|
2023-08-16 18:02:41 +08:00
|
|
|
|
if (_panelItems[i].btnImg != null) _panelItems[i].btnImg.sprite = null;//hide the button image
|
2015-02-10 08:03:38 +08:00
|
|
|
|
_panelItems[i].img.sprite = item.Image;
|
|
|
|
|
_panelItems[i].img.color = (item.Image == null) ? new Color(1, 1, 1, 0)
|
|
|
|
|
: item.IsDisabled ? new Color(1, 1, 1, .5f)
|
|
|
|
|
: Color.white;
|
|
|
|
|
int ii = i; //have to copy the variable for use in anonymous function
|
|
|
|
|
_panelItems[i].btn.onClick.RemoveAllListeners();
|
|
|
|
|
_panelItems[i].btn.onClick.AddListener(() =>
|
|
|
|
|
{
|
|
|
|
|
OnItemClicked(ii);
|
|
|
|
|
if (item.OnSelect != null) item.OnSelect();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
_panelItems[i].gameobject.SetActive(i < Items.Count);// if we have more thanks in the panel than Items in the list hide them
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnItemClicked(int indx)
|
|
|
|
|
{
|
2017-05-17 01:51:24 +08:00
|
|
|
|
//Debug.Log("item " + indx + " clicked");
|
2017-03-27 20:33:08 +08:00
|
|
|
|
if (indx != _selectedIndex && OnSelectionChanged != null) OnSelectionChanged.Invoke(indx);
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
|
|
|
|
_selectedIndex = indx;
|
|
|
|
|
ToggleDropdownPanel(true);
|
|
|
|
|
UpdateSelected();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateSelected()
|
|
|
|
|
{
|
|
|
|
|
SelectedItem = (_selectedIndex > -1 && _selectedIndex < Items.Count) ? Items[_selectedIndex] : null;
|
|
|
|
|
if (SelectedItem == null) return;
|
|
|
|
|
|
|
|
|
|
bool hasImage = SelectedItem.Image != null;
|
|
|
|
|
if (hasImage)
|
|
|
|
|
{
|
|
|
|
|
_mainButton.img.sprite = SelectedItem.Image;
|
|
|
|
|
_mainButton.img.color = Color.white;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_mainButton.img.sprite = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mainButton.txt.text = SelectedItem.Caption;
|
|
|
|
|
|
2016-07-15 01:48:18 +08:00
|
|
|
|
//update selected index color
|
|
|
|
|
if (OverrideHighlighted)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _itemsPanelRT.childCount; i++)
|
|
|
|
|
{
|
|
|
|
|
_panelItems[i].btnImg.color = (_selectedIndex == i) ? _mainButton.btn.colors.highlightedColor : new Color(0, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
private void RedrawPanel()
|
2015-02-10 08:03:38 +08:00
|
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
|
float scrollbarWidth = _panelItems.Count > ItemsToDisplay ? _scrollBarWidth : 0f;//hide the scrollbar if there's not enough items
|
|
|
|
|
_scrollBarRT.gameObject.SetActive(_panelItems.Count > ItemsToDisplay);
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
float dropdownHeight = _itemsToDisplay > 0 ? _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count) : _rectTransform.sizeDelta.y * _panelItems.Count;
|
|
|
|
|
dropdownHeight += dropdownOffset;
|
|
|
|
|
|
|
|
|
|
if (!_hasDrawnOnce || _rectTransform.sizeDelta != _mainButton.rectTransform.sizeDelta)
|
2015-02-10 08:03:38 +08:00
|
|
|
|
{
|
|
|
|
|
_hasDrawnOnce = true;
|
|
|
|
|
_mainButton.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _rectTransform.sizeDelta.x);
|
|
|
|
|
_mainButton.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, _rectTransform.sizeDelta.y);
|
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
var itemsRemaining = _panelItems.Count - ItemsToDisplay;
|
|
|
|
|
itemsRemaining = itemsRemaining < 0 ? 0 : itemsRemaining;
|
|
|
|
|
|
|
|
|
|
_scrollPanelRT.SetParent(transform, true);
|
|
|
|
|
_scrollPanelRT.anchoredPosition = _displayPanelAbove ?
|
|
|
|
|
new Vector2(0, dropdownOffset + dropdownHeight) :
|
|
|
|
|
new Vector2(0, -(dropdownOffset + _rectTransform.sizeDelta.y));
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
//make the overlay fill the screen
|
|
|
|
|
_overlayRT.SetParent(_canvas.transform, false);
|
2015-02-10 08:03:38 +08:00
|
|
|
|
_overlayRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _canvasRT.sizeDelta.x);
|
|
|
|
|
_overlayRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, _canvasRT.sizeDelta.y);
|
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
_overlayRT.SetParent(transform, true);
|
|
|
|
|
_scrollPanelRT.SetParent(_overlayRT, true);
|
2015-02-10 08:03:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
if (_panelItems.Count < 1) return;
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
2023-02-07 22:35:43 +08:00
|
|
|
|
_scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight);
|
2015-02-10 08:03:38 +08:00
|
|
|
|
_scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _rectTransform.sizeDelta.x);
|
|
|
|
|
|
|
|
|
|
_itemsPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _scrollPanelRT.sizeDelta.x - scrollbarWidth - 5);
|
|
|
|
|
_itemsPanelRT.anchoredPosition = new Vector2(5, 0);
|
|
|
|
|
|
|
|
|
|
_scrollBarRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, scrollbarWidth);
|
|
|
|
|
_scrollBarRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight);
|
2021-05-11 01:38:31 +08:00
|
|
|
|
if (scrollbarWidth == 0) _scrollHandleRT.gameObject.SetActive(false); else _scrollHandleRT.gameObject.SetActive(true);
|
2015-02-10 08:03:38 +08:00
|
|
|
|
|
|
|
|
|
_slidingAreaRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 0);
|
|
|
|
|
_slidingAreaRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight - _scrollBarRT.sizeDelta.x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Toggle the drop down list
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="directClick"> whether an item was directly clicked on</param>
|
|
|
|
|
public void ToggleDropdownPanel(bool directClick)
|
|
|
|
|
{
|
2023-02-07 22:35:43 +08:00
|
|
|
|
if (!isActive) return;
|
2023-08-16 18:02:41 +08:00
|
|
|
|
|
2015-08-22 10:56:47 +08:00
|
|
|
|
_overlayRT.transform.localScale = new Vector3(1, 1, 1);
|
|
|
|
|
_scrollBarRT.transform.localScale = new Vector3(1, 1, 1);
|
2015-02-10 08:03:38 +08:00
|
|
|
|
_isPanelActive = !_isPanelActive;
|
|
|
|
|
_overlayRT.gameObject.SetActive(_isPanelActive);
|
|
|
|
|
if (_isPanelActive)
|
|
|
|
|
{
|
|
|
|
|
transform.SetAsLastSibling();
|
|
|
|
|
}
|
|
|
|
|
else if (directClick)
|
|
|
|
|
{
|
2023-08-16 18:02:41 +08:00
|
|
|
|
// scrollOffset = Mathf.RoundToInt(itemsPanelRT.anchoredPosition.y / _rectTransform.sizeDelta.y);
|
2015-02-10 08:03:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-07 22:35:43 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates the control and sets its active status, determines whether the dropdown will open ot not
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="status"></param>
|
|
|
|
|
public void SetActive(bool status)
|
|
|
|
|
{
|
|
|
|
|
if (status != isActive)
|
|
|
|
|
{
|
|
|
|
|
OnControlDisabled?.Invoke(status);
|
|
|
|
|
}
|
|
|
|
|
isActive = status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|