最近项目看到一个大佬写的比较好的动态增删的scrollview 列表,唯一有些缺陷就是无法居中, 我把他整理下分享给大家
demo: https://download.csdn.net/download/weixin_41843959/10603338
列表个数始终保持再自己设置的个数,滑动时通过把显示外看不到的子物体重新设置位置,初始化属性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIListTest : MonoBehaviour {
private UIHorizontal mList2;
int[] intlist = { 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 20010, 20011, 20012, 20013, 20014, 20015, 20016, 20017, 20018, 20019 };
void Start ()
{
mList2 = transform.Find("Horizontal").GetComponent();
//mList.Init(50, 50, 2, 3);
mList2.InitData();
mList2.InitList(intlist.Length, (item, index) =>
{//子物体属性赋值
item.name = index.ToString();
item.Find("Text").GetComponent().text = intlist[index].ToString();
});
}
}
横向列表控制类
属性设置:
固定节点
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// /// 横向列表
///
public class UIHorizontal : MonoBehaviour
{
// 外部基础参数设置
public int itemWidth; //单元格宽
public int itemHeight; //单元格高
public int columnCount; // 显示列数
public int rowCount; // 显示行数
public int offsetX = 0; //列间隔
public int offsetY = 0; //行间隔
//基础设置
private ScrollRect scrollRect;
private RectTransform itemParent;
private GameObject itemObj; //克隆体
private Vector2 m_maskSize; //遮罩大小
//根据参数来计算
private int m