在这里插入图片描述

你是否是这样?

在这里插入图片描述
Content高度 < 全部Cell加在一起的总高

他就认为你的全部Cell加起来就跟Content一样大,所以才出现了这种完全回弹

我该怎么办?

在这里插入图片描述
很简单,改变Content的长度跟所有Cell的和一样大

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组件上

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);
    }
}