循环列表
首先在FGUI界面搭建UI并导入Unity,这里省略搭UI的步骤
playerList = GetChild("playerList").asList; //获取到列表
获取到列表后,设置虚拟循环列表
playerList.SetVirtualAndLoop();
playerList.numItems = 5; //设置列表长度
渲染的方法进行注册
playerList.itemRenderer = RenderListItem;
void RenderListItem(int index, GObject obj)
{
GButton item = obj.asButton;
item.SetPivot(0.5f, 0.5f); //设置中心点
//这里可以使用装载器的icon进行配置
}
这里有个onScroll事件可以监听,这个事件会在我们滚动列表的过程中触发
playerList.scrollPane.onScroll.Add(Test); //在这里我监听一个Test方法,后面会贴出Test方法
这里Test方法主要地方给出了注释
void Test()
{
float listCenter = playerList.scrollPane.posX + playerList.viewWidth / 2; //得到列表的中心点(FGUI默认锚点在左上角)
//所以,列表的中心点x坐标=列表的x坐标(默认在左上角)+列表宽度的一半
int viewItemCount = playerList.numChildren;
for(int i = 0; i < viewItemCount; i++)
{
GObject item = playerList.GetChildAt(i);
float itemCenter = item.x + item.width / 2; //得到item的中心点
float distance = Mathf.Abs(listCenter - itemCenter); //得到列表中心点到item中心点的距离
if (distance > item.width) //距离大于item的宽度
{
item.SetScale(0.8f, 0.8f);
}
else
{
float scale = 0.8f + (1 - distance / item.width) * 0.2f;
item.SetScale(scale, scale);
}
}
}
这里特别要注意:numChildren和numItem在循环列表中是不一样的
numChildren:指的是在视图中显示出来的item的个数
numItem:不只是显示出来的item数量,还包括未显示的item数量