From e085cbe0765dd040232c06c504f6cadcfb3a4c4e Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Mon, 6 Feb 2023 13:32:05 +0000 Subject: [PATCH] Resolved issues with DisplayAbove and using a 0 ItemsToDisplay --- .../Controls/ComboBox/AutoCompleteComboBox.cs | 10 ++++++---- Runtime/Scripts/Controls/ComboBox/ComboBox.cs | 10 ++++++---- .../Scripts/Controls/ComboBox/DropDownList.cs | 20 ++++++++++--------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Runtime/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs b/Runtime/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs index d5708d8..f329b97 100644 --- a/Runtime/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs +++ b/Runtime/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs @@ -369,6 +369,10 @@ namespace UnityEngine.UI.Extensions { float scrollbarWidth = _panelItems.Count > ItemsToDisplay ? _scrollBarWidth : 0f;//hide the scrollbar if there's not enough items _scrollBarRT.gameObject.SetActive(_panelItems.Count > ItemsToDisplay); + + float dropdownHeight = _itemsToDisplay > 0 ? _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count) : _rectTransform.sizeDelta.y * _panelItems.Count; + dropdownHeight += dropdownOffset; + if (!_hasDrawnOnce || _rectTransform.sizeDelta != _inputRT.sizeDelta) { _hasDrawnOnce = true; @@ -380,8 +384,8 @@ namespace UnityEngine.UI.Extensions _scrollPanelRT.SetParent(transform, true); _scrollPanelRT.anchoredPosition = _displayPanelAbove ? - new Vector2(0, dropdownOffset + _rectTransform.sizeDelta.y * (_panelItems.Count - itemsRemaining) - 1) : - new Vector2(0, -_rectTransform.sizeDelta.y); + new Vector2(0, dropdownOffset + dropdownHeight) : + new Vector2(0, -(dropdownOffset + _rectTransform.sizeDelta.y)); //make the overlay fill the screen _overlayRT.SetParent(_canvas.transform, false); @@ -394,8 +398,6 @@ namespace UnityEngine.UI.Extensions if (_panelItems.Count < 1) return; - float dropdownHeight = _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count) + dropdownOffset; - _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight); _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _rectTransform.sizeDelta.x); diff --git a/Runtime/Scripts/Controls/ComboBox/ComboBox.cs b/Runtime/Scripts/Controls/ComboBox/ComboBox.cs index b790f57..094f4aa 100644 --- a/Runtime/Scripts/Controls/ComboBox/ComboBox.cs +++ b/Runtime/Scripts/Controls/ComboBox/ComboBox.cs @@ -280,6 +280,10 @@ namespace UnityEngine.UI.Extensions { float scrollbarWidth = _panelItems.Count > ItemsToDisplay ? _scrollBarWidth : 0f;//hide the scrollbar if there's not enough items _scrollBarRT.gameObject.SetActive(_panelItems.Count > ItemsToDisplay); + + float dropdownHeight = _itemsToDisplay > 0 ? _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count) : _rectTransform.sizeDelta.y * _panelItems.Count; + dropdownHeight += dropdownOffset; + if (!_hasDrawnOnce || _rectTransform.sizeDelta != _inputRT.sizeDelta) { _hasDrawnOnce = true; @@ -291,8 +295,8 @@ namespace UnityEngine.UI.Extensions _scrollPanelRT.SetParent(transform, true); _scrollPanelRT.anchoredPosition = _displayPanelAbove ? - new Vector2(0, dropdownOffset + _rectTransform.sizeDelta.y * (_panelItems.Count - itemsRemaining) - 1) : - new Vector2(0, -_rectTransform.sizeDelta.y); + new Vector2(0, dropdownOffset + dropdownHeight) : + new Vector2(0, -(dropdownOffset + _rectTransform.sizeDelta.y)); //make the overlay fill the screen _overlayRT.SetParent(_canvas.transform, false); @@ -305,8 +309,6 @@ namespace UnityEngine.UI.Extensions if (_panelItems.Count < 1) return; - float dropdownHeight = _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count) + dropdownOffset; - _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight); _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _rectTransform.sizeDelta.x); diff --git a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs index d50f24d..7de1b07 100644 --- a/Runtime/Scripts/Controls/ComboBox/DropDownList.cs +++ b/Runtime/Scripts/Controls/ComboBox/DropDownList.cs @@ -363,7 +363,11 @@ namespace UnityEngine.UI.Extensions { float scrollbarWidth = _panelItems.Count > ItemsToDisplay ? _scrollBarWidth : 0f;//hide the scrollbar if there's not enough items _scrollBarRT.gameObject.SetActive(_panelItems.Count > ItemsToDisplay); - if (!_hasDrawnOnce || _rectTransform.sizeDelta != _mainButton.rectTransform.sizeDelta) + + 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) { _hasDrawnOnce = true; _mainButton.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _rectTransform.sizeDelta.x); @@ -373,12 +377,12 @@ namespace UnityEngine.UI.Extensions itemsRemaining = itemsRemaining < 0 ? 0 : itemsRemaining; _scrollPanelRT.SetParent(transform, true); - _scrollPanelRT.anchoredPosition = _displayPanelAbove ? - new Vector2(0, dropdownOffset + _rectTransform.sizeDelta.y * (_panelItems.Count - itemsRemaining) - 1) : - new Vector2(0, -_rectTransform.sizeDelta.y); + _scrollPanelRT.anchoredPosition = _displayPanelAbove ? + new Vector2(0, dropdownOffset + dropdownHeight) : + new Vector2(0, -(dropdownOffset + _rectTransform.sizeDelta.y)); - //make the overlay fill the screen - _overlayRT.SetParent(_canvas.transform, false); + //make the overlay fill the screen + _overlayRT.SetParent(_canvas.transform, false); _overlayRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _canvasRT.sizeDelta.x); _overlayRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, _canvasRT.sizeDelta.y); @@ -388,9 +392,7 @@ namespace UnityEngine.UI.Extensions if (_panelItems.Count < 1) return; - float dropdownHeight = _rectTransform.sizeDelta.y * Mathf.Min(_itemsToDisplay, _panelItems.Count) + dropdownOffset; - - _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight); + _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, dropdownHeight); _scrollPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _rectTransform.sizeDelta.x); _itemsPanelRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _scrollPanelRT.sizeDelta.x - scrollbarWidth - 5);