【发布时间】:2021-08-25 15:53:11
【问题描述】:
我有一个容器 (1),我想随其内容一起增长。
在这个 Container (1) 内,我有一个 Scrollrect (2),我也想随其内容一起增长,但要达到最大高度。
目标是如果(2)中只有一行文字,(1)会小很多。当添加大量文本时,(2) 将增长直到达到其最大高度,滚动条将接管。
我已经做了好几个小时了,但我似乎找不到让它工作的方法。
【问题讨论】:
我有一个容器 (1),我想随其内容一起增长。
在这个 Container (1) 内,我有一个 Scrollrect (2),我也想随其内容一起增长,但要达到最大高度。
目标是如果(2)中只有一行文字,(1)会小很多。当添加大量文本时,(2) 将增长直到达到其最大高度,滚动条将接管。
我已经做了好几个小时了,但我似乎找不到让它工作的方法。
【问题讨论】:
您可以通过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;
}
}
【讨论】:
我通过将以下脚本添加到视口来实现它:
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,但是检查器不会显示我的新 MaxHeight 和 ContentToGrowWith 变量。
ContentToGrowWith 变量指向 ViewPort 内的内容对象。
最后的设置是:
Control Child Size: "Height" 的滚动视图。 (没有子力扩展,所以它可以和它的孩子一起收缩)LayoutElement 和我的脚本AdaptablePreferredHeight 的视口。Vertical Fit: "Preferred Size" 上设置ContentSizeFitter 的内容@
【讨论】: