Unity FairyGUI(九)
一.组件拓展类
之前写的UI代码没有面向对象的概念,写起来非常的烦乱,FGUI也可以实现面向对象的写法
- 创建MyUIPanel类,继承自GComponent
- 实现重写方法ConstructFromXML(Xml xml)
- 在此方法中初始化相应的组件,添加相应的相应事件
- 使用时需要UIObjectFactory.SetPackageItemExtension()初始化
public class MyUIPanel : GComponent
{
private GButton _gButton;
private GTextInput _gTextInput;
public override void ConstructFromXML(XML xml)
{
base.ConstructFromXML(xml);
_gButton = GetChild("n1").asButton;
_gButton.onClick.Add(() =>
{
Debug.Log("按钮点击!");
});
_gTextInput = GetChild("n4").asTextInput;
}
public void ShowInfo(string str)
{
_gTextInput.text = str;
}
}
UIObjectFactory.SetPackageItemExtension("ui://Package1/LearnFGUI4",typeof(MyUIPanel));
var gObj = UIPackage.CreateObject("Package1", "LearnFGUI4").asCom;
var myPanel = (MyUIPanel)(gObj.asCom);
GRoot.inst.AddChild(myPanel);
myPanel.ShowInfo("zzs");
写这种方法不太便捷,FGUI也提供了生成代码的功能,可以自动生成代码,须在FGUI编辑器中提前设置好导出代码的设置
Package1Binder.BindAll();
var learnFui4 = UI_LearnFGUI4.CreateInstance();
GRoot.inst.AddChild(learnFui4);
learnFui4.ShowInfo("Hello!!!");
二.分辨率自适应
-
GRoot.inst.SetContentScaleFactor(1365,768,UIContentScaler.ScreenMatchMode.MatchHeight);
-
learnFui4.AddRelation(GRoot.inst,RelationType.Size);
-
learnFui4.MakeFullScreen();
-
Package1Binder.BindAll();
var learnFui4 = UI_LearnFGUI4.CreateInstance();
learnFui4.AddRelation(GRoot.inst,RelationType.Size);
learnFui4.MakeFullScreen();
GRoot.inst.AddChild(learnFui4);
learnFui4.ShowInfo("Hello!!!");
|