Unity FairyGUI(九)

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!!!");
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值