同样是关于一段代码的理解:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
public class FairyGUIText3 : MonoBehaviour
{
private GComponent mainUI;
private GList list;
// Start is called before the first frame update
void Start()
{
mainUI = GetComponent<UIPanel>().ui;
list = mainUI.GetChild("n1").asList;
list.SetVirtualAndLoop();
list.itemRenderer = RenderListItem;
list.numItems = 5;
list.scrollPane.onScroll.Add(SetCenter);
SetCenter();
}
private void RenderListItem(int index, GObject gObject)
{
GButton gButton = gObject.asButton;
gButton.icon = UIPackage.GetItemURL("Package5", "n" + (index + 1));
gButton.SetPivot(0.5f, 0.5f);
}
void SetCenter()
{
float listCenter = list.scrollPane.posX + list.viewWidth / 2;
for (int i = 0; i < list.numChildren; i++)
{
GObject item = list.GetChildAt(i);
float itemCenter = item.x + item.width / 2;
float itemWidth = item.width;
float distance = Mathf.Abs(itemCenter - listCenter);
if (distance < itemWidth)
{
float needScale = 1 + (1 - distance / itemWidth) * 0.2f;
item.SetScale(needScale, needScale);
}
else
{
item.SetScale(1, 1);
}
}
}
}
list.SetVirtualAndLoop();
将list设置为虚拟和循环列表,关于虚拟和循环列表可以看FairyGUI的教程
list.itemRenderer = RenderListItem;
为列表添加渲染函数,也就是列表进行渲染的时候会执行RenderListItem函数
list.numItems = 5;
设置列表内元件的个数,对应于渲染函数中的index,但注意index是从0开始的;若为虚拟列表,该值代表显示的部分。
list.scrollPane.onScroll.Add(SetCenter);
为列表滑动时添加函数
PS:注意列表滑动的函数和渲染的函数只需在Start中调用一次即可,不用在Update中每帧都调用,具体实现应该看内部封装
gButton.icon = UIPackage.GetItemURL("Package5", "n" + (index + 1));
加载器加载相应包中的资源,参数分别为包名和资源名
gButton.SetPivot(0.5f, 0.5f);
设置gButton元件的轴心
float listCenter = list.scrollPane.posX + list.viewWidth / 2;
scrollPane.posX是滑动的距离,viewWidth是列表中显示范围的宽度,注意x是列表位置,width是列表宽度
for (int i = 0; i < list.numChildren; i++)
numChildren这个是当前显示的子元件的数量,一般回合numItems相等
GObject item = list.GetChildAt(i);
通过序号得到子元件