Laya FairyGui系列二 GObject GComponent

GObject

GObject是FGUI中UI组件的基类。GComponent,GImage,GGraph等都继承自GObject。
但GObject虽是FGUI的组件基类,但是FGUI的所有组件还是在Laya的基础上进行封装的。例如每个GObject对象或者GObject子类的对象中都包含一个Laya.Sprite对象

    handleXYChanged() {
            var xv = this._x;
            var yv = this._y + this._yOffset;
            if (this._pivotAsAnchor) {
                xv -= this._pivotX * this._width;
                yv -= this._pivotY * this._height;
            }
            if (this._pixelSnapping) {
                xv = Math.round(xv);
                yv = Math.round(yv);
            }
            this._displayObject.pos(xv + this._pivotOffsetX, yv + this._pivotOffsetY);
    }

关于轴心点
设置轴心点的源码:

     setPivot(xv, yv = 0, asAnchor = false) {
            if (this._pivotX != xv || this._pivotY != yv || this._pivotAsAnchor != asAnchor) {
                this._pivotX = xv;
                this._pivotY = yv;
                this._pivotAsAnchor = asAnchor;
                this.updatePivotOffset();
                this.handleXYChanged();
            }
        }
      updatePivotOffset() {
            if (this._displayObject != null) {
                if (this._displayObject.transform && (this._pivotX != 0 || this._pivotY != 0)) {
                    GObject.sHelperPoint.x = this._pivotX * this._width;
                    GObject.sHelperPoint.y = this._pivotY * this._height;
                    var pt = this._displayObject.transform.transformPoint(GObject.sHelperPoint);
                    this._pivotOffsetX = this._pivotX * this._width - pt.x;
                    this._pivotOffsetY = this._pivotY * this._height - pt.y;
                }
                else {
                    this._pivotOffsetX = 0;
                    this._pivotOffsetY = 0;
                }
            }
        }

FGUI的轴心点和Laya的有一点区别,如果设置一个120*120的图片的轴心点为中心时Laya应设置为(60,60),FGUI应设置为(0.5,0.5)。并且FGUI中不允许不设置轴心点的情况下直接设置锚点。

GComponent

FGUI中GComponent是非常常用的基础容器,可以包含多个基础显示对象或者包含其他GComponent。例如按钮,列表,进度条等UI组件也都是继承自GComponent。GComponent是一个容器,所以包含_children属性,可以被当成父节点,GObject则不可以。

GComponent的使用

  • 创建
const testCom = fairygui.UIPackage.createObject('包名','组件名').asCom
  • 销毁
testCom.dispose();
  • 操作子节点(子节点相关的方法有很多,看下源码吧)


    Laya FairyGui系列二 GObject GComponent_第1张图片
    GComponent.png

    其中包括按名称,按zIndex,按ID等获取子节点,添加子节点,删除子节点,交换子节点的zIndex等方法。

实例运用中我习惯将每个独立的页面创建成GComponent。项目开发中很多UI是以非全屏弹框的形式展示,FGUI中的窗口系统可以实现弹框,但是个人觉得使用的限制比较多,在美术人员把非全屏改全屏或者全屏改非全屏的时候修改的地方比较多。

你可能感兴趣的:(Laya FairyGui系列二 GObject GComponent)