• ScrollView的OnValueChanged


    关于限制scrollView滚动

    • 这个限制滚动的需求,其实只能放置在LateUpdate函数里面进行操作
    • ScrollView在update里面进行了移动,在lateupdate里面判断位置是否超过限制,并进行卡死
    • 但是我为了图方便,在别人写好的接口里面进行开发,结果只要滑动速度过快,就会无法卡住。
    计算某个具体元素的位置
    • 因为这涉及到计算在ScrollView中某个元素在列表中的位置。
    • 其实还是蛮难计算的。
    • 这里以垂直滚动为例子。
    • 首先我们要算出滚动的距离
    滚动的距离 = content的高度-视口的高度
    
    • 1
    • value = 计算第i个元素的高度/滚动距离

    • 再把scrollRect的VerticalNormalization设置为这个值

    • 一个坑

    using UnityEngine;
    using UnityEngine.UI;
    
    public class ScrollDistanceCalculator : MonoBehaviour
    {
        public ScrollRect scrollRect;
    
        void Update()
        {
            // 获取视口的大小
            Vector2 viewportSize = new Vector2(scrollRect.viewport.rect.width, scrollRect.viewport.rect.height);
    
            // 获取内容的大小
            Vector2 contentSize = new Vector2(scrollRect.content.rect.width, scrollRect.content.rect.height);
    
            // 获取当前的归一化位置
            Vector2 normalizedPosition = scrollRect.normalizedPosition;
    
            // 计算像素距离
            float horizontalDistance = (contentSize.x - viewportSize.x) * normalizedPosition.x;
            float verticalDistance = (contentSize.y - viewportSize.y) * normalizedPosition.y;
    
            // 打印像素距离
            Debug.Log("Horizontal Distance: " + horizontalDistance);
            Debug.Log("Vertical Distance: " + verticalDistance);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 我在设置的时候使用的是
     Debug.Log($"ViewPort height_{GetComponent<ScrollRect>().viewport.rect.height}");
    Debug.Log($"ViewPort height_{GetComponent<ScrollRect>().viewport.sizeDelta.y}");
    
    • 1
    • 2
    • ViewPort的SizeDelta和Rect的height是不一样的。这就涉及到另一个知识点。SizeDelta是什么,Rect又是什么

    • 滚动到第i个元素的实现

    using System.Collections;
    using System.Collections.Generic;
    using TMPro;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class Test : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
    
        }
    
        public enum Align
        {
            Top,
            Middle,
            Bottom,
        }
        public Align align;
    
        public int ItemIndex;
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.K))
            {
                Debug.Log($"content height_{GetComponent<ScrollRect>().content.rect.height}");
                Debug.Log($"content size_Y{GetComponent<ScrollRect>().content.sizeDelta.y}");
    
                Debug.Log($"content height_{GetComponent<ScrollRect>().viewport.rect.height}");
                Debug.Log($"content height_{GetComponent<ScrollRect>().viewport.sizeDelta.y}");
    
    
            }
    
            if (Input.GetKeyDown(KeyCode.R))
            {
                var btns = GetComponent<ScrollRect>().content.GetComponentsInChildren<UnityEngine.UI.Button>();
                int index = 0;
                foreach (var item in btns)
                {
                    item.transform.Find("Text").GetComponent<TextMeshProUGUI>().text = $"{index++}";
                }
                var sumHeight = GetComponent<ScrollRect>().content.rect.height - GetComponent<ScrollRect>().viewport.rect.height;
                float value = 0;
                for (int i = 0; i < ItemIndex; i++)
                {
                    value += btns[i].GetComponent<RectTransform>().rect.height;
                }
    
                switch (align)
                {
                    case Align.Top:
                        value = value;
                        break;
                    case Align.Middle:
                        value = value + btns[ItemIndex].GetComponent<RectTransform>().rect.height / 2;
                        break;
                    case Align.Bottom:
                        value = value + btns[ItemIndex].GetComponent<RectTransform>().rect.height;
    
                        break;
                    default:
                        break;
                }
                GetComponent<ScrollRect>().verticalNormalizedPosition = 1 - value / sumHeight;
            }
    
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
  • 相关阅读:
    从零开始探索C语言(八)----指针
    Java+JSP基于ssm高校网上教材征订系统-计算机毕业设计
    90. 子集 II
    Java中实现线程等待和唤醒总结
    gdb mi接口命令入门大全
    互联网扫地僧精心总结,39W字上千道Java一线大厂面试题手册
    Centos7 安装Seata1.5.1
    牛客小白月赛52 E 分组求对数和(容斥定理+二分)
    计算点云每个点的高斯曲率(附open3d python代码)
    【Proteus仿真】【STM32单片机】出租车计价器
  • 原文地址:https://blog.csdn.net/qq_33574890/article/details/133222374