当前位置:网站首页>Unity笔记——FairyGUI
Unity笔记——FairyGUI
2022-07-30 00:28:00 【گ这是一个QQ名字】
前言
平时一直用的都是Unity内部的UGUI来着,本来以为FairyGUI跟UGUI差不多,用了之后才发现完全不是一个等级的,FairyGUI用起来舒服多了,很多Unity内部处理比较麻烦的FairyGUI都封装好了
Guide - 滚动容器 - 《FairyGUI 教程》 - 书栈网 · BookStack
代码和UI制作可参考官方demo
使用
FairyGUI生成面板的两种方式,一种是Unity鼠标右键直接新建面板
一种是动态加载,使用Resources或者是AB包加载,这里为了测试直接使用Resources了
1:可以直接给物体挂载UIPanel ,在设置其组件(也可以不这么做)
UIPanel panel = yourGameObject.AddComponent<UIPanel>();panel.packageName = “包名”;
panel.componentName = “组件名”;
2:也可以设置全局节点控制所有面板,
比如:按钮,动画测试


using System.Net.Mime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using DG.Tweening;
public class Test3 : MonoBehaviour
{
//GRoot GRoot = GRoot.inst;
GComponent ButtonPanel;
GComponent AniTestPanel;
void Start()
{
GRoot.inst.SetContentScaleFactor(800, 600);
UIPackage.AddPackage("A1");
ButtonPanel = UIPackage.CreateObject("A1", "Component1").asCom;
ButtonPanel.GetChild("n02").onClick.Add(Click1);
GRoot.inst.AddChild(ButtonPanel);
}
private void Update()
{
Debug.Log(GRoot.inst.touchTarget);
if (Stage.isTouchOnUI) //点在了UI上
{
//Debug.Log(Stage.inst.touchPosition);
Debug.Log("点在了UI上");
}
else //没有点在UI上
{
Debug.Log("没有点在UI上");
}
}
void Click1()
{
if (AniTestPanel == null)
{
AniTestPanel = UIPackage.CreateObject("A1", "C3").asCom;
}
GRoot.inst.AddChild(AniTestPanel);
Transition ani = AniTestPanel.GetTransition("t0");
ani.SetHook("AddValue", () =>
{
int startValue = 9000000;
GTextField text = AniTestPanel.GetChild("n1").asTextField;
DOTween.To(() => startValue, changValue => { text.text = Mathf.Floor(changValue).ToString(); }, 9999999, 2f);
});
ani.Play(() => GRoot.inst.RemoveChild(AniTestPanel));
}
}新手指导遮罩测试:FairyGUI拖拖拽拽就可以做一个

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
public class TEST4 : MonoBehaviour
{
GComponent componentC1;
GComponent testBtn;
GComponent guidbtn;
GObject n8;
/// <summary>
/// mask测试
/// </summary>
void Start()
{
componentC1 = GetComponent<UIPanel>().ui;
testBtn = componentC1.GetChild("testBtn").asButton;
guidbtn = componentC1.GetChild("guidBtn").asButton;
n8 = componentC1.GetChild("n8");
testBtn.onClick.Add(() =>
{
n8.visible = true;
});
guidbtn.onClick.Add(() =>
{
Debug.Log("asdsad");
componentC1.GetChild("n8").visible = false;
});
}循环列表
新建一个按钮,按钮下新建一个装载器起名字为icon(按钮特殊名)
新建一个组件,组件新建一个list,List添加几个我们的按钮就可以了,然后list别忘记设置一个默认资源,否则会报错
新建几个图片,(你要给按钮的图片),图片名字设置好方便Unity查找,
设置图片和组件都要为导出,好让我们动态加载

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
/// <summary>
/// LoopList
/// </summary>
public class TEST5 : MonoBehaviour
{
GComponent mainUI;
GList list;
// Start is called before the first frame update
void Start()
{
mainUI = GetComponent<UIPanel>().ui;
list = mainUI.GetChild("n2").asList;
list.SetVirtualAndLoop();
list.itemRenderer = Rend;
list.numItems = 4;
}
void Rend(int index, GObject obj)
{
GButton button = obj.asButton;
button.icon = UIPackage.GetItemURL("A3", "Image" + index);
}
}
button.SetPivot(0.5f, 0.5f);设置list内部物品居中
设置居中之后,list内物品大小会超过边界,如果将List大小调大,但是其实并没有作用
必须设置物品和list,list和物体之间的相互关联才可以
所有最终如果你想让物品按照居中显示放大缩小
1.第一点需要先设置锚点在中间
2.FGUI内部设置list足够大小
3.设置list容器关联
4.设置你的loader容器关联

效果
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
/// <summary>
/// LoopList
/// </summary>
public class TEST5 : MonoBehaviour
{
GComponent mainUI;
GList list;
// Start is called before the first frame update
void Start()
{
mainUI = GetComponent<UIPanel>().ui;
list = mainUI.GetChild("n2").asList;
list.SetVirtualAndLoop();
list.itemRenderer = Rend;
list.numItems = 5;
//滚动事件注册
list.scrollPane.onScroll.Add(Slide);
}
void Rend(int index, GObject obj)
{
GButton button = obj.asButton;
button.SetPivot(0.5f, 0.5f);
button.icon = UIPackage.GetItemURL("A3", "Image" + (index + 1));
}
void Slide()
{
Debug.Log("我在滑动");
//change the scale according to the distance to middle
float midX = list.scrollPane.posX + list.viewWidth / 2;
int cnt = list.numChildren;
for (int i = 0; i < cnt; i++)
{
GObject obj = list.GetChildAt(i);
float dist = Mathf.Abs(midX - obj.x - obj.width / 2);
if (dist > obj.width) //no intersection
obj.SetScale(1, 1);
else
{
float ss = 1 + (1 - dist / obj.width) * 0.24f;
obj.SetScale(ss, ss);
}
}
}
}
如果不想让它一直一直循环, list.SetVirtualAndLoop()删除即可
//list.scrollPane.inertiaDisabled = false;惯性关闭
//list.scrollPane.pageMode = true;页面播放
个人感觉FairyGUI一些功能还是很实用的,比如新手指引,翻书效果,循环列表,一些UI的显示效果等等
边栏推荐
猜你喜欢

WeChat developer tools set the tab size to 2

Unity Addressables

Since the media increase play a short video?From the three aspects

谷歌浏览器(google)设置翻译中文,翻译选项不生效或没有弹出翻译选项

Worthington优化技术:细胞定量

Selenium上传文件有多少种方式?不信你有我全

Recurrent Neural Network (RNN)

KDE Frameworks 5.20.0:Plasma迎来诸多改进

Elephant Swap: Provide arbitrage space in the crypto market with ePLATO

“ 我是一名阿里在职9年软件测试工程师,我的经历也许能帮到处于迷茫期的你 ”
随机推荐
Based on TNEWS 'today's headline news in Chinese short text classification
重新定义分析 - EventBridge 实时事件分析平台发布
He cell separation technology 丨 basic primary cell separation methods and materials
Chinese semantic matching
Print linked list from end to beginning
Worthington解离酶:胰蛋白酶及常见问题
rk-boot framework combat (1)
canvas 中如何实现物体的框选(六)
How to increase account weight?3 ways to operate your own media to help you get more revenue
Recurrent Neural Network (RNN)
Douyin short video traffic acquisition strategy, mastering these will definitely be a hit
新闻文本分类
Genesis与Axis Ventures互动密切
二叉排序树(C语言)
Worthington用于细胞收获的胰蛋白酶&细胞释放程序
【集训DAY16】KC ‘ s Stars【dfs】
测试员容易陷入的9大误区
ZLMediaKit源码学习——UDP
从尾到头打印链表
直播平台搭建,设置状态栏颜色