【问题标题】:Unity: How to make a scrollrect fit its content with a maximum height?Unity:如何使滚动矩形适合其内容的最大高度?
【发布时间】:2021-08-25 15:53:11
【问题描述】:

我有一个容器 (1),我想随其内容一起增长。

在这个 Container (1) 内,我有一个 Scrollrect (2),我也想随其内容一起增长,但要达到最大高度。

目标是如果(2)中只有一行文字,(1)会小很多。当添加大量文本时,(2) 将增长直到达到其最大高度,滚动条将接管。

我已经做了好几个小时了,但我似乎找不到让它工作的方法。

【问题讨论】:

    标签: unity3d unity-ui


    【解决方案1】:

    您可以通过Text.preferredHeight 进行此操作,例如喜欢这个组件在你的滚动内容上

    public class ContentScaler : MonoBehaviour
    {
        [SerializeField] private RectTransform _scrollRectTransform;
        [SerializeField] private RectTransform _contentRectTransform;
        [SerializeField] private Text _text;
    
        [SerializeField] private float minScrollHeight;
        [SerializeField] private float maxScrollHeight;
    
        private void Awake()
        {
            if (!_contentRectTransform) _contentRectTransform = GetComponent<RectTransform>();
        }
    
        private void Update()
        {
            // Get the preferred Height the text component would require in order to 
            // display all available text
            var desiredHeight = _text.preferredHeight;
    
            // actually make your text have that height
            var textSizeDelta = _text.rectTransform.sizeDelta;
            textSizeDelta.y = desiredHeight;
            _text.rectTransform.sizeDelta = textSizeDelta;
    
            // Then make the content have the same height
            var contentSizeDelta = _contentRectTransform.sizeDelta;
            contentSizeDelta.y = desiredHeight;
            _contentRectTransform.sizeDelta = contentSizeDelta;
    
            var scrollSizeDelta = _scrollRectTransform.sizeDelta;
            scrollSizeDelta.y = Mathf.Clamp(desiredHeight, minScrollHeight, maxScrollHeight);
            _scrollRectTransform.sizeDelta = scrollSizeDelta;
        }
    }
    

    【讨论】:

    • 谢谢,但在您的示例中,滚动视图已经以最大尺寸开始。想要实现的是一个滚动视图,从小开始,然后在内容增长到最大高度时增长。
    • @RyanPergent 嘿,很抱歉这么晚才回来!查看更新后的答案,现在应该可以满足您的要求了
    【解决方案2】:

    我通过将以下脚本添加到视口来实现它:

    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    
    [RequireComponent(typeof(LayoutElement))]
    public class AdaptablePreferredHeight : UIBehaviour, ILayoutElement
    {
        public float MaxHeight = 100;
        public RectTransform ContentToGrowWith;
    
        public int LayoutPriority = 10;
    
        LayoutElement m_LayoutElement;
        float m_Preferredheight;
    
        public float minWidth => m_LayoutElement.minWidth;
        public float preferredWidth => m_LayoutElement.preferredWidth;
        public float flexibleWidth => m_LayoutElement.flexibleWidth;
        public float minHeight => m_LayoutElement.minHeight;
        public float preferredHeight => m_Preferredheight;
        public float flexibleHeight => m_LayoutElement.flexibleHeight;
        public int layoutPriority => LayoutPriority;
    
        public void CalculateLayoutInputHorizontal()
        {
            if (m_LayoutElement == null)
            {
                m_LayoutElement = GetComponent<LayoutElement>();
            }
        }
    
        public void CalculateLayoutInputVertical()
        {
            if(m_LayoutElement == null)
            {
                m_LayoutElement = GetComponent<LayoutElement>();
            }
    
            float contentHeight = ContentToGrowWith.sizeDelta.y;
    
            if (contentHeight < MaxHeight)
            {
                m_Preferredheight = contentHeight;
            }
            else
            {
                m_Preferredheight = MaxHeight;
            }
        }
    
    }
    

    我会直接继承 LayoutElement 而不是将其设置为 RequireComponent,但是检查器不会显示我的新 MaxHeightContentToGrowWith 变量。

    ContentToGrowWith 变量指向 ViewPort 内的内容对象。

    最后的设置是:

    • 具有水平布局组Control Child Size: "Height" 的滚动视图。 (没有子力扩展,所以它可以和它的孩子一起收缩)
    • 带有LayoutElement 和我的脚本AdaptablePreferredHeight 的视口。
    • Vertical Fit: "Preferred Size" 上设置ContentSizeFitter 的内容@

    【讨论】:

      猜你喜欢
      • 2011-06-11
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 2014-11-08
      • 1970-01-01
      • 2014-09-04
      相关资源
      最近更新 更多