当前位置:网站首页>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中运行就没有任何问题了。
十、最终完美效果图
边栏推荐
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- (课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
- Classification, understanding and application of common methods of JS array
- Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
- Générateur d'identification distribué basé sur redis
- NRF24L01故障排查
- 1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
- ORA-02030: can only select from fixed tables/views
- Acwing-116 pilot brother
- Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
猜你喜欢
Basic operations of databases and tables ----- view data tables
基于Redis的分布式锁 以及 超详细的改进思路
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
JS function promotion and declaration promotion of VaR variable
2021.11.10 compilation examination
Symbolic representation of functions in deep learning papers
JS正则表达式基础知识学习
Redis cache update strategy, cache penetration, avalanche, breakdown problems
Vulnhub target: hacknos_ PLAYER V1.1
dosbox第一次使用
随机推荐
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
Single chip Bluetooth wireless burning
Learning notes of JS variable scope and function
JS function promotion and declaration promotion of VaR variable
Arduino gets the length of the array
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
Basic operations of databases and tables ----- view data tables
基於Redis的分布式ID生成器
Important methods of array and string
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
Talking about the startup of Oracle Database
[Leetcode15]三数之和
2021.11.10汇编考试
js 变量作用域和函数的学习笔记
[Offer18]删除链表的节点
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
Redis based distributed ID generator
燕山大学校园网自动登录问题解决方案
SSD technical features