你是否是这样?
Content高度 < 全部Cell加在一起的总高
他就认为你的全部Cell加起来就跟Content一样大,所以才出现了这种完全回弹
我该怎么办?
很简单,改变Content的长度跟所有Cell的和一样大文章来源:https://www.toymoban.com/news/detail-843351.html
void RefreshSize()
{
float allDelta = 0;
foreach (RectTransform rect in content)
{
allDelta += rect.sizeDelta.y;
}
allDelta += emptyDistance;
content.sizeDelta = new Vector2(content.sizeDelta.x, allDelta);
}
更简单一点的组件方式,挂到Layout组件上文章来源地址https://www.toymoban.com/news/detail-843351.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(RectTransform))]
public class ScrollViewContentSizeController : MonoBehaviour
{
public enum LayoutType
{
Vertical,
Horizontal,
VerticalAndHorizontal
}
//结尾的空隙长度
[SerializeField] float emptyDistance = 0.1f;
[SerializeField] LayoutType layoutType = LayoutType.Vertical;
RectTransform content;
void Awake()
{
content = GetComponent<RectTransform>();
}
//自行决定是否Update调用
void Update()
{
RefreshSize(layoutType);
}
public void RefreshSize(LayoutType layoutType)
{
switch (layoutType)
{
case LayoutType.Vertical:
RefreshVerticalSize();
break;
case LayoutType.Horizontal:
RefreshHorizontalSize();
break;
case LayoutType.VerticalAndHorizontal:
RefreshVerticalSize();
RefreshHorizontalSize();
break;
}
}
void RefreshVerticalSize()
{
float allDelta = 0;
foreach (RectTransform rect in content)
{
allDelta += rect.sizeDelta.y;
}
allDelta += emptyDistance;
content.sizeDelta = new Vector2(content.sizeDelta.x, allDelta);
}
void RefreshHorizontalSize()
{
float allDelta = 0;
foreach (RectTransform rect in content)
{
allDelta += rect.sizeDelta.x;
}
allDelta += emptyDistance;
content.sizeDelta = new Vector2(allDelta, content.sizeDelta.y);
}
void RefreshVerticalAndHorizontalSize()
{
float allDeltaX = 0;
float allDeltaY = 0;
foreach (RectTransform rect in content)
{
allDeltaX += rect.sizeDelta.x;
allDeltaY += rect.sizeDelta.y;
}
allDeltaX += emptyDistance;
allDeltaY += emptyDistance;
content.sizeDelta = new Vector2(allDeltaX, allDeltaY);
}
}
到了这里,关于Unity 3D ScrollRect和ScrollView回弹问题的解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!