Unity FairyGUI笔记
跨引擎UI工具,首先我想到,做好的面板不合适要在工具里调整,重新导出、导入,是否需要两个软件和代码编辑器都开着?如果再开一个教程的浏览器,内存是否够用?要么只开一个,两个软件轮流打开?
基本概念
上来我们会遇到大量概念:
编辑器里的Package、Component。
Unity API里的
UIPackage、UIPanel、GRoot、GComponent、GObject
Package
含有多个Component(面板预制体)和美术资源的资源包,类比AB包去理解。
Component
一个面板。
编辑器
轴心0,0代表左上角。未勾选作为锚点时轴心只是旋转中心,锚点还是左上角,勾选后位置是轴心相对于面板左上角的位置(和UGUI一样)。
最小尺寸、最大尺寸:
作为子对象时的限制。
API
常用方法:
UIPackage.AddPackage(packageName);加载包
UIPackage.CreateObject(packageName, componentName);加载组件
GRoot.inst.AddChild(component);加给根
component.GetChild("元件名");得到元件
常用组件类:
GComponent
GImage
GGraph图形
GMovieClip序列帧动画
GLoader装载器
得到使用GetChile().as元件类型。创建使用UIPackage.CreateObject("包名", "组件名").as类型名,除了图形用graph=new GGraph();然后component.AddChild(graph);
查看gObject的游戏对象
gObject.displayObject.gameObject
调整分辨率适配
void SetScale(int width, int height){GRoot.inst.SetContentScaleFactor(width, height,UIContentScaler.ScreenMatchMode.MatchHeight);// GRoot.inst.AddChild();}
加载包及其依赖包
void LoadPackage(string packageName){UIPackage package = UIPackage.AddPackage(packageName);//从Resources加载//AddPackage只有用到才会加载资源// package.LoadAllAssets();//加载所有资源foreach (var i in package.dependencies){UIPackage.AddPackage(i["name"]);//加载依赖包}}
通过UIPanel加载面板
void LoadUIPanel(string name, string packageName, string componentName){GameObject obj = new GameObject(name);obj.layer = LayerMask.NameToLayer("UI");FairyGUI.UIPanel panel = obj.AddComponent<FairyGUI.UIPanel>();panel.packageName = packageName;panel.componentName = componentName;panel.CreateUI();panel.container.renderMode = RenderMode.ScreenSpaceOverlay;panel.container.fairyBatching = true;panel.SetHitTestMode(HitTestMode.Default);}
通过GComponent加载面板:CreateObject输入包名、组件名,AddChild加给根。
void LoadPanel(string packageName, string componentName){GComponent component = UIPackage.CreateObject(packageName, componentName).asCom;GRoot.inst.AddChild(component);}
UIPackage.CreateObject填包名和包里的组件、资源名。
通过GComponent异步加载面板:在委托里加给根。
void LoadPanelAsync(string packageName, string componentName){UIPackage.CreateObjectAsync(packageName, componentName, (obj) =>{GComponent component = obj.asCom;GRoot.inst.AddChild(component);});}
GetChild("元件名");得到元件。这里是序列帧动画。
GMovieClip clip = component.GetChild("n8").asMovieClip;//填在组件里的名字nX
GetChild填组件里的元件名:
创建一个序列帧动画。
GMovieClip clip2 = UIPackage.CreateObject(packageName,"Zombie").asMovieClip;
GRoot.inst.AddChild(clip2);
clip2.SetPosition(0, 0,0);
这些组件名、元件名都要去FairyGUI编辑器里看,Unity里看不到。