04-初始FairyGUI

同样是关于一段代码的理解:

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);

通过序号得到子元件

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值