Unity 3D ScrollRect和ScrollView回弹问题的解决

在这里插入图片描述

你是否是这样?

在这里插入图片描述
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);
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YUE ZHEN PENG

码字不易,如果你想请我喝杯果汁

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值