当前位置:网站首页>FairyGUI循环列表
FairyGUI循环列表
2022-07-06 09:18:00 【SQ刘】
FairyGUI循环列表
实现效果:在Unity中,通过鼠标可以循环浏览列表,并且浏览当前页时,有一个近大远小的效果。

一、导入资源





二、新建按钮

三、设计列表


四、装载器动态加载图片按钮
如果有上万个按钮,我们不可能一个个创建,这时候就需要在Unity中动态加载图片按钮的资源,故此引入装载器。
按钮大小是248×378,那么装载器也应该是248×378。然后将其位置归为0,0。

再次进入列表,给它多添加几个对象。
五、打包发布


六、Unity中显示


七、编写代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
public class LoopList : MonoBehaviour
{
private GComponent mainUI;
private GList list;
void Start()
{
mainUI = GetComponent<UIPanel>().ui;
list = mainUI.GetChild("n0").asList; //n0是FairyGUI中的列表
list.SetVirtualAndLoop(); //把列表设置为虚拟列表(FGUI规定:如果使用列表,则必须使用虚拟列表)
list.itemRenderer = RenderListItem; //调用list里面的渲染函数,它是一个委托类型的变量,所以要给它赋值一个方法
list.numItems = 5; //设置列表长度
}
private void RenderListItem(int index, GObject obj)
{
GButton button = obj.asButton;
button.icon = UIPackage.GetItemURL("LoopList", "n" + (index + 1)); //动态获取URL
}
}
编译运行,回到Unity发现报错。原来在FairyGUI中忘记拖入项目资源了,手动拖入即可。

再次重新发布一下
此时,回到Unity,运行结果如下。发现“第二章”、“第三章”下边的图片都是“第一章”下边的图片。什么原因呢?因为之前我们在FairyGUI中给了Button1一个渲染,显然这是不可以的。所以我们要把它本身的图片渲染给删掉。直接通过icon对它加载就可以了。


还有一个地方就是,找到LoopList列表,把它的溢出处理设置为“水平滚动”。
再次发布,运行一切正常。
八、运行效果

九、进一步完善——近大远小的特效
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
public class LoopList : MonoBehaviour
{
private GComponent mainUI;
private GList list;
void Start()
{
mainUI = GetComponent<UIPanel>().ui;
list = mainUI.GetChild("n0").asList; //n0是FairyGUI中的列表
list.SetVirtualAndLoop(); //把列表设置为虚拟列表(FGUI规定:如果使用列表,则必须使用虚拟列表)
list.itemRenderer = RenderListItem; //调用list里面的渲染函数,它是一个委托类型的变量,所以要给它赋值一个方法
list.numItems = 5;
list.scrollPane.onScroll.Add(DoSpecialEffect);
DoSpecialEffect();
}
private void DoSpecialEffect()
{
float listCenter = list.scrollPane.posX + list.viewWidth / 2;
//scrollPane是当前列表的宽度;viewWidth是列表自身宽度
for (int i = 0; i < list.numChildren; i++) //numChildren是当前渲染的对象,也就是运行时Game中能看见的个数
{
GObject item = list.GetChildAt(i);
float itemCenter = item.x + item.width / 2;
float itemWidth = item.width;
float distance = Mathf.Abs(listCenter - itemCenter);
if (distance < itemWidth)
{
float distanceRange = 1 + (1 - distance / itemWidth) * 0.2f;
item.SetScale(distanceRange, distanceRange);
}
else
{
item.SetScale(1, 1);
}
}
}
private void RenderListItem(int index, GObject obj)
{
GButton button = obj.asButton;
button.SetPivot(0.5f, 0.5f); //设置锚点为中心位置
button.icon = UIPackage.GetItemURL("LoopList", "n" + (index + 1)); //动态获取URL
}
}

将列表和按钮分别与容器做一下关联

发布后在Unity中运行就没有任何问题了。
十、最终完美效果图

边栏推荐
- idea中好用的快捷键
- [leetcode19]删除链表中倒数第n个结点
- Idea problem record
- Symbolic representation of functions in deep learning papers
- Navigator object (determine browser type)
- Redis based distributed locks and ultra detailed improvement ideas
- [leetcode19] delete the penultimate node in the linked list
- MySQL时间、时区、自动填充0的问题
- JUC forkjoin and completable future
- Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
猜你喜欢

Whistle+switchyomega configure web proxy

Générateur d'identification distribué basé sur redis

CUDA C programming authoritative guide Grossman Chapter 4 global memory

Learning notes of JS variable scope and function

Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect

Pytorch: tensor operation (I) contiguous

Symbolic representation of functions in deep learning papers

数据库课程设计:高校教务管理系统(含代码)

JS数组常用方法的分类、理解和运用

Latex learning
随机推荐
Types de variables JS et transformations de type communes
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
基于Redis的分布式ID生成器
程序设计大作业:教务管理系统(C语言)
JS變量類型以及常用類型轉換
单片机蓝牙无线烧录
Redis based distributed locks and ultra detailed improvement ideas
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
[leetcode622]设计循环队列
InnoDB dirty page refresh mechanism checkpoint in MySQL
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
Game 280 weekly
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
How to add music playback function to Arduino project
Programming homework: educational administration management system (C language)
Database course design: college educational administration management system (including code)
CUDA C programming authoritative guide Grossman Chapter 4 global memory
FairyGUI简单背包的制作
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)