lcy
2025-12-03 b9a6e7e896b451e9c915e782a1789b2afe079cc9
Main/Component/UI/EnhancedScroller/EnhancedScroller.cs
@@ -262,7 +262,7 @@
                        // set the vertical position
                        scrollRect.verticalNormalizedPosition = 1f - (_scrollPosition / _ScrollSize);
                        //这里要注意,如果集合进行过复制,那么这里的_scrollSize也一样会比原来的增加,因为_ScrollSize永远比content的总大小小一个可视区大小
                    }
                    else
                    {
@@ -379,9 +379,9 @@
        /// The scroller should only be moving one one axix.
        /// </summary>
        public float LinearVelocity //实质上是操作ScrollRect.velocity
                       //Get : 将向量转为float类型
                       //Set:用一个float类型构造一个相应的向量
        //外部方法可以通过访问这个属性来使scroll运动起来
                                    //Get : 将向量转为float类型
                                    //Set:用一个float类型构造一个相应的向量
                                    //外部方法可以通过访问这个属性来使scroll运动起来
        {
            get
            {
@@ -450,7 +450,7 @@
                return _activeCellViewsStartIndex % NumberOfCells;
            }
        }
        /// <summary>
        /// This is the last data index showing in the scroller's visible area
        /// </summary>
@@ -462,8 +462,10 @@
            }
        }
        public int ActiveCellCnt {
            get {
        public int ActiveCellCnt
        {
            get
            {
                return _activeCellViews.Count;
            }
        }
@@ -512,7 +514,7 @@
                var go = Instantiate(cellPrefab.gameObject);
                cellView = go.GetComponent<EnhancedScrollerCellView>();
                cellView.transform.SetParent(_container);
            }
            Vector3 pos = cellView.transform.localPosition;
            if (pos.x == float.NaN || pos.y == float.NaN)
@@ -881,9 +883,12 @@
        /// Cached reference to the scrollRect
        /// </summary>
        private ScrollRect m_ScrollRect;
        public ScrollRect scrollRect {
            get {
                if (m_ScrollRect == null) {
        public ScrollRect scrollRect
        {
            get
            {
                if (m_ScrollRect == null)
                {
                    m_ScrollRect = this.GetComponent<ScrollRect>();
                }
                return m_ScrollRect;
@@ -1075,10 +1080,10 @@
        /// of the scroller
        /// </summary>
        public float _ScrollSize//这里并不是指scrllrect的大小,而是指真正在滑动的区域的大小
                                /*scrollrect的normallizePosition实际上就是这个真正能滑动的区域的大小
                                 * 在滑动过程中归一化的值,而这个大小就是所有子物体的大小与可视化区域的差
                                 * 当content的大小等于所有子物体的大小时,就可以简化操作直接用content的大小来代替
                                 */
        /*scrollrect的normallizePosition实际上就是这个真正能滑动的区域的大小
         * 在滑动过程中归一化的值,而这个大小就是所有子物体的大小与可视化区域的差
         * 当content的大小等于所有子物体的大小时,就可以简化操作直接用content的大小来代替
         */
        {
            get
            {
@@ -1090,7 +1095,7 @@
                        return 0;
                }
                else
                {
                    if (_container != null && _scrollRectTransform != null)
@@ -1098,9 +1103,88 @@
                    else
                        return 0;
                }
            }
        }
        // 仅聊天功能使用
        public void AddHeight(bool keepPosition, float height)
        {
            // 如果启用循环,回退到完整的 _Resize,因为循环逻辑复杂
            if (loop)
            {
                _Resize(keepPosition);
                return;
            }
            // 缓存原始位置
            var originalScrollPosition = _scrollPosition;
            int cellCount = NumberOfCells;
            if (cellCount == 0)
            {
                // 没有单元格,调用 _Resize 处理
                _Resize(keepPosition);
                return;
            }
            int newCellIndex = cellCount - 1; // 新单元格的索引
            // 检查内部数组是否一致:_cellViewSizeArray 应该已经包含了除新单元格外的所有单元格
            if (_cellViewSizeArray.Count != newCellIndex)
            {
                // 不一致,回退到完整重置
                _Resize(keepPosition);
                return;
            }
            // 计算新单元格的大小(包括间距)
            float newSize = height;
            newSize += spacing;
            // 添加到大小数组
            _cellViewSizeArray.Add(newSize);
            // 更新偏移数组
            float previousOffset = (_cellViewOffsetArray.Count > 0) ? _cellViewOffsetArray.Last() : 0;
            _cellViewOffsetArray.Add(previousOffset + newSize);
            // 设置活动单元格容器的大小
            if (scrollDirection == ScrollDirectionEnum.Vertical)
                _container.sizeDelta = new Vector2(_container.sizeDelta.x, _cellViewOffsetArray.Last() + padding.top + padding.bottom);
            else
                _container.sizeDelta = new Vector2(_cellViewOffsetArray.Last() + padding.left + padding.right, _container.sizeDelta.y);
            // 重建可见单元格
            _ResetVisibleCellViews();
            if (this.Delegate != null)
            {
                this.Delegate.OnRebuildComplete();
            }
            // 如果需要保持原始位置
            if (keepPosition)
            {
                ScrollPosition = originalScrollPosition;
            }
            else
            {
                ScrollPosition = 0;
            }
            // 设置滚动条可见性
            ScrollbarVisibility = scrollbarVisibility;
            if (!m_IsLoadAll)
            {
                m_IsLoadAll = true;
                if (OnFirstLoadAllEvent != null)
                {
                    OnFirstLoadAllEvent();
                }
            }
        }
        /// <summary>
        /// This function will create an internal list of sizes and offsets to be used in all calculations.
@@ -1157,7 +1241,7 @@
            {
                _loopFirstScrollPosition = GetScrollPositionForCellViewIndex(_loopFirstCellIndex, CellViewPositionEnum.Before) + (spacing * 0.5f);
                _loopLastScrollPosition = GetScrollPositionForCellViewIndex(_loopLastCellIndex, CellViewPositionEnum.After) - ScrollRectSize + (spacing * 0.5f);
                _loopFirstJumpTrigger = _loopFirstScrollPosition - ScrollRectSize;
                _loopLastJumpTrigger = _loopLastScrollPosition + ScrollRectSize;
@@ -1399,10 +1483,10 @@
        /// <param name="cellIndex">The index of the cell view</param>
        /// <param name="listPosition">Whether to add the cell to the beginning or the end</param>
        private void _AddCellView(int cellIndex, ListPositionEnum listPosition)
        {
        {
            //通过某个索引添加(从缓冲池中取)子物体到_activeCellViews集合中,并触发相关事件
            //这个方法本身也会根据传过来的索引来初始化CellView
            if (NumberOfCells == 0) return;
            // get the dataIndex. Modulus is used in case of looping so that the first set of cells are ignored
@@ -1662,7 +1746,7 @@
            _lastScrollbarVisibility = scrollbarVisibility;
            //一些变量进行了初始化
            if(OnCompLoad!=null) OnCompLoad();
            if (OnCompLoad != null) OnCompLoad();
            inited = true;
        }
@@ -1672,7 +1756,7 @@
            {
                // if the reload flag is true, then reload the data
                ReloadData();//理论上不用在外部调用ReloadData()因为当成功赋值Delegate后这个方法就会执行一次
                               //但如果想在update之前执行就主动调用ReloadData()
                             //但如果想在update之前执行就主动调用ReloadData()
            }
            // if the scroll rect size has changed and looping is on,
@@ -1770,7 +1854,7 @@
             */
        }
        /// <summary>
        /// This is fired by the tweener when the snap tween is completed
        /// </summary>