using DG.Tweening;
using XLua;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 子物体横向移动左上对齐,纵向移动左上对齐
/// </summary>
public class ScrollableAreaController : MonoBehaviour
{
public ScrollableCell cellPrefab;
private RectTransform content;
private RectTransform viewport;
[SerializeField] private int NUMBER_OF_COLUMNS = 1; //表示并排显示几个,比如是上下滑动,当此处为2时表示一排有两个cell
private float cellWidth;
private float cellHeight;
[SerializeField] private float cellSpace = 0f; //cell的间距
[SerializeField] private float gridSpace = 0f;//组间距
[SerializeField] private bool isInitAnim = false;//生成动画
private int visibleCellsTotalCount = 0;
private int visibleCellsRowCount = 0;
private LinkedList<GameObject> localCellsPool = new LinkedList<GameObject>(); //备用cell
private LinkedList<GameObject> cellsInUse = new LinkedList<GameObject>(); //被用到的cell
private ScrollRect rect;
//private IList allCellsData ;
private int cellDataCount = 0; //列表数据长度
private int previousInitialIndex = 0; //先前第一个cell的序号
private int initialIndex = 0; //当前第一个cell的序号
private float initpostion = 0;
//下拉刷新
private Action mTopAction = null;
private bool needTopRefresh = false;
//上拉刷新
private Action mBottomAction = null;
private bool needBotRefresh = false;
private LuaTable mLuaTable = null;
private bool isShow = false;
public void PreInit()
{
rect = GetComponent<ScrollRect>();
content = rect.content;
viewport = rect.viewport;
if (NUMBER_OF_COLUMNS == 0) NUMBER_OF_COLUMNS = 1;
if (NUMBER_OF_COLUMNS == 1) gridSpace = 0;
RectTransform cell = cellPrefab.GetComponent<RectTransform>();
cellWidth = cell.sizeDelta.x;
cellHeight = cell.sizeDelta.y;
content.anchorMin = Vector2.up;
content.anchorMax = Vector2.up;
content.pivot = Vector2.up;
cell.anchorMin = Vector2.up;
cell.anchorMax = Vector2.up;
cell.pivot = Vector2.up;
}
public void Init(LuaTable luatable)
{
PreInit();
content.anchoredPosition = Vector2.zero;
if (horizontal)
visibleCellsRowCount = Mathf.CeilToInt(viewport.rect.size.x / (cellWidth + cellSpace));
else
visibleCellsRowCount = Mathf.CeilToInt(viewport.rect.size.y / (cellHeight + cellSpace));
visibleCellsTotalCount = visibleCellsRowCount + 1;
visibleCellsTotalCount *= NUMBER_OF_COLUMNS;
mLuaTable = luatable;
CreateCellPool();
}
public void AddPageCallback(Action topAction,
unity 优化 ScrollView
于 2022-03-19 18:45:38 首次发布