一只腊鸭做游戏

Laya FairyGui系列六 GLoader

2019-11-20  本文已影响0人  s0ok

装载器(GLoader)

FGUI规定需要切换图片的Texture时就需要使用装载器,所以装载器在FGUI中的使用也非常广泛,例如前面说到的GButton的图形标题。

创建装载器的方法和创建图形一样,在左侧工具栏点击装载器按钮创建一个装载器。 GLoader.png

装载器的属性:

装载器时继承自GObject,但是自身并不会处理图片的显示过程,自身只处理指定的显示内容的加载:

__proto.loadContent=function(){
       this.clearContent();
       if (!this._url)
           return;
       if(ToolSet.startsWith(this._url,"ui://"))
           this.loadFromPackage(this._url);
       else
       this.loadExternal();
   }

   __proto.loadExternal=function(){
       AssetProxy.inst.load(this._url,Handler.create(this,this.__getResCompleted),null,"image");
   }
   __proto.onExternalLoadSuccess=function(texture){
       this._content.texture=texture;
       this._content.scale9Grid=null;
       this._content.scaleByTile=false;
       this._contentSourceWidth=texture.width;
       this._contentSourceHeight=texture.height;
       this.updateLayout();
   }

通过源码可以看到,在设置装载器的url属性时会判断如果设置的值是"ui://"开头则同FGUI资源包中寻找指定的资源,或者则通过Laya.loader加载指定的资源。加载完成后将加载的内容赋值给MovieClip,由MovieClip来负责内容的显示。
这里不说MovieClip如何处理内容显示的,如果有兴趣可以点击这里

装载器的使用

const testLoader = new fairygui.GLoader();
//一定要设置装载器大小,或者显示不出来
testLoader.setSize(100,100);
testLoader.autoSize = true;
testCom.addChild(testLoader);
//设置资源包中的图片(URL)
testLoader.icon = "ui://pz4zm4twoxk59";
//设置资源包中的图片(包名/图片或者组件名)
testLoader.icon = "ui://Package1/skill015";
//设置本地res目录下的资源(res下完成路径)
testLoader.icon = "res/skill014.png";
//设置本地图集中的资源(图集名称/图片名称)
testLoader.icon = "skillIcon/skill015.png";

设置装载器的url是本地资源时会先去内存里面查找资源是否已经加载,如果未加载则会执行Laya.loader的加载过程,如果指定的是图集那么加载整个图集。

testLoader.onClick(this,()=>{
    console.log("点击了GLoader");
})
const testLoader2 = testCom.getChild("n22").asLoader;
const testButton = testLoader2.component.asButton;
testButton.onClick(this,()=>{
    console.log("点击了GLoader中的Button");
})

注:

上一篇 下一篇

猜你喜欢

热点阅读