| | |
| | | // set the vertical position
|
| | | scrollRect.verticalNormalizedPosition = 1f - (_scrollPosition / _ScrollSize);
|
| | | //这里要注意,如果集合进行过复制,那么这里的_scrollSize也一样会比原来的增加,因为_ScrollSize永远比content的总大小小一个可视区大小
|
| | | |
| | |
|
| | | }
|
| | | else
|
| | | {
|
| | |
| | | /// 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
|
| | | {
|
| | |
| | | return _activeCellViewsStartIndex % NumberOfCells;
|
| | | }
|
| | | }
|
| | | |
| | |
|
| | | /// <summary>
|
| | | /// This is the last data index showing in the scroller's visible area
|
| | | /// </summary>
|
| | |
| | | }
|
| | | }
|
| | |
|
| | | public int ActiveCellCnt {
|
| | | get {
|
| | | public int ActiveCellCnt
|
| | | {
|
| | | get
|
| | | {
|
| | | return _activeCellViews.Count;
|
| | | }
|
| | | }
|
| | |
| | | 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)
|
| | |
| | | /// 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;
|
| | |
| | | /// of the scroller
|
| | | /// </summary>
|
| | | public float _ScrollSize//这里并不是指scrllrect的大小,而是指真正在滑动的区域的大小
|
| | | /*scrollrect的normallizePosition实际上就是这个真正能滑动的区域的大小
|
| | | * 在滑动过程中归一化的值,而这个大小就是所有子物体的大小与可视化区域的差
|
| | | * 当content的大小等于所有子物体的大小时,就可以简化操作直接用content的大小来代替
|
| | | */
|
| | | /*scrollrect的normallizePosition实际上就是这个真正能滑动的区域的大小
|
| | | * 在滑动过程中归一化的值,而这个大小就是所有子物体的大小与可视化区域的差
|
| | | * 当content的大小等于所有子物体的大小时,就可以简化操作直接用content的大小来代替
|
| | | */
|
| | | {
|
| | | get
|
| | | {
|
| | |
| | | return 0;
|
| | |
|
| | | }
|
| | | |
| | |
|
| | | else
|
| | | {
|
| | | if (_container != null && _scrollRectTransform != null)
|
| | |
| | | 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.
|
| | |
| | | {
|
| | | _loopFirstScrollPosition = GetScrollPositionForCellViewIndex(_loopFirstCellIndex, CellViewPositionEnum.Before) + (spacing * 0.5f);
|
| | | _loopLastScrollPosition = GetScrollPositionForCellViewIndex(_loopLastCellIndex, CellViewPositionEnum.After) - ScrollRectSize + (spacing * 0.5f);
|
| | | |
| | |
|
| | |
|
| | | _loopFirstJumpTrigger = _loopFirstScrollPosition - ScrollRectSize;
|
| | | _loopLastJumpTrigger = _loopLastScrollPosition + ScrollRectSize;
|
| | |
| | | /// <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
|
| | |
| | | _lastScrollbarVisibility = scrollbarVisibility;
|
| | | //一些变量进行了初始化
|
| | |
|
| | | if(OnCompLoad!=null) OnCompLoad();
|
| | | if (OnCompLoad != null) OnCompLoad();
|
| | | inited = true;
|
| | | }
|
| | |
|
| | |
| | | {
|
| | | // 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,
|
| | |
| | | */
|
| | |
|
| | | }
|
| | | |
| | |
|
| | | /// <summary>
|
| | | /// This is fired by the tweener when the snap tween is completed
|
| | | /// </summary>
|