溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Egret之RES資源加載

發(fā)布時間:2020-09-10 06:07:01 來源:網(wǎng)絡(luò) 閱讀:15317 作者:Aonaufly 欄目:開發(fā)技術(shù)

一 , 獲取資源的3種方式:

①:RES.getRes(name:string):any

同步獲取資源 這種方式只能獲取已經(jīng)緩存過的資源


②:RES.getResAsync(name:string,compFunc:Function,thisObject:any):void

異步獲取資源,這種方式可以獲取配置中含有的所有資源項。如果緩存中存在,直接調(diào)用回調(diào)函數(shù)返回,若不存在,就啟動網(wǎng)絡(luò)加載文件并解析后回調(diào)。


③:RES.getResByUrl(url:string,compFunc:Function,thisObject:any,type:string=””):void

通過url獲取不在配置中的資源,通常不建議使用這個接口,只有那些不合適填寫在配置中,比如獲取網(wǎng)絡(luò)上其他服務(wù)器的資源時,才采用這種方式。

    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event:egret.Event) {
        RES.getResByUrl('/resource/assets/bg.jpg',this.onComplete,this,RES.ResourceItem.TYPE_IMAGE);
    }

    private onComplete(event:any):void {
        var img: egret.Texture = <egret.Texture>event;
        var bitmap: egret.Bitmap = new egret.Bitmap(img);
        this.addChild(bitmap);
    }



二 , 加載資源組的步驟

1 : 加載資源配置json文件,如resouce.res.json.

RES.addEventListener( RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this ); 
RES.addEventListener( RES.ResourceEvent.CONFIG_LOAD_ERROR, this.onConfigLoadErr, this ); 
RES.loadConfig("resources/resource.res.json","resources/");

解析 :

①:RES.loadConfig

                    參數(shù)1:json配置文件的路徑 。 參數(shù)2 : 子資源的位置


2: 加載json配置中的某一個資源組 , 比如加載preload資源組

private onConfigComplete(event: RES.ResourceEvent): void {
    RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
    RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
    RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
    RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
    RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
    RES.loadGroup("preload");
}

3:偵聽加載事件:

/**
 * preload資源組加載完成
 */
private onResourceLoadComplete(event: RES.ResourceEvent) {
    if (event.groupName == "preload") {
        this.stage.removeChild(this.loadingView);
        RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
    }
}

/**
 * 資源組加載出錯
 */
private onItemLoadError(event: RES.ResourceEvent) {
    console.warn("Url:" + event.resItem.url + " has failed to load");
}

/**
 * 資源組加載出錯
 */
private onResourceLoadError(event: RES.ResourceEvent) {
    console.warn("Group:" + event.groupName + " has failed to load");
    //忽略加載失敗的項目
    this.onResourceLoadComplete(event);
}

/**
 * preload資源組加載進(jìn)度
 */
private onResourceProgress(event: RES.ResourceEvent) {
    if (event.groupName == "preload") {
        this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
    }
}


三 , 自定義加載組加載資源(重點)

在json中建立資源,這次資源不放在任何組中,本人使用代碼自動構(gòu)建組進(jìn)行加載

Egret之RES資源加載


加載管理器:

module app{
    /**
     * 構(gòu)建組RES并加載
     * @author Aonaufly
     */
    export class AutoGroupResLoardManager{
        private _loaderArr : Array<IAutoGroupResLoarData> = null;
        private _is_loading : boolean = false;//是否正在加載中
        private static _instance : AutoGroupResLoardManager = null;
        /**當(dāng)前正在加載的資源數(shù)據(jù)*/
        private _cur_data : IAutoGroupResLoarData = null;
        public static get Ins() : AutoGroupResLoardManager{
            if( AutoGroupResLoardManager._instance == null ) AutoGroupResLoardManager._instance = new AutoGroupResLoardManager();
            return AutoGroupResLoardManager._instance;
        }
        public constructor(  ){
            this._loaderArr = [];//數(shù)據(jù)加載的集合
        }

        /**
         * 開始加載接口
         * @param {app.IAutoGroupResLoarData} data
         */
        public startLoader( data : IAutoGroupResLoarData ) : void{
            if( this._is_loading ){
                this._loaderArr.push( data );//放入加載列表等待加載
            }else{
                this.createGroup2Res( data );
            }
        }

        /**
         * 構(gòu)建組并加載
         * @param {app.IAutoGroupResLoarData} data
         */
        private createGroup2Res( data : IAutoGroupResLoarData ) : void{
            if( RES.createGroup( data._groupName , data._arr2Res , true )){
                this._cur_data = data;
                this.onConfigComplete(data);
            }else{
                //構(gòu)建失敗
                data._callBack( TypeCallBack2AutoGroupResLoar.CREATE_GROUP_ERROR );
                this.loaderNext();//繼續(xù)加載下一個
            }
        }

        /**
         * 加載下一個
         */
        private loaderNext() : void{
            if( this._loaderArr.length > 0 ){
                let data : IAutoGroupResLoarData = this._loaderArr.shift();
                this.createGroup2Res( data );
            }else{
                RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.onResourceLoad, this);
                RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR,this.onResourceLoad, this);
                RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS,this.onResourceLoad, this);
                RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR,this.onResourceLoad, this);
            }
        }

        /**
         * 添加加載偵聽
         * @param {app.IAutoGroupResLoarData} data
         */
        private onConfigComplete(data : IAutoGroupResLoarData): void {
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoad, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoad, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceLoad, this);
            RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onResourceLoad, this);
            RES.loadGroup(data._groupName);
        }

        /**
         * 加載偵聽處理
         * @param {RES.ResourceEvent} $e
         */
        private onResourceLoad( $e : RES.ResourceEvent ):void{
            switch( $e.type ){
                case RES.ResourceEvent.GROUP_COMPLETE:
                    if( $e.groupName == this._cur_data._groupName ){
                        this._cur_data._callBack( TypeCallBack2AutoGroupResLoar.GROUP_COMPLETE , this._cur_data._class );//加載完成
                        this.loaderNext();//加載下一個
                    }
                    break;
                case RES.ResourceEvent.GROUP_LOAD_ERROR:
                    if( $e.groupName == this._cur_data._groupName ){
                        this._cur_data._callBack( TypeCallBack2AutoGroupResLoar.GROUP_COMPLETE , this._cur_data._class);//容錯加載完成
                        this.loaderNext();
                    }
                    break;
                case RES.ResourceEvent.GROUP_PROGRESS://加載進(jìn)度
                    if($e.groupName == this._cur_data._groupName){
                        this._cur_data._callBack( TypeCallBack2AutoGroupResLoar.GROUP_PROGRESS , this._cur_data._class ,$e );
                    }
                    break;
                case RES.ResourceEvent.ITEM_LOAD_ERROR:
                    if($e.groupName == this._cur_data._groupName){
                        this._cur_data._callBack( TypeCallBack2AutoGroupResLoar.ITEM_LOAD_ERROR , this._cur_data._class ,$e);
                        console.log("Url:" + $e.resItem.url + " has failed to load");
                    }
                    break;
            }
        }
    }

    /**
     * 自動加載數(shù)據(jù)
     * @author Aonaufly
     */
    export interface IAutoGroupResLoarData{
        /**
         * 組名
         */
        _groupName : string;
        /**
         * 資源名
         */
        _arr2Res : Array<string>;
        /**
         * 回調(diào)方法
         */
        _callBack : Function;
        _class : any;
    }

    /**
     * 毀掉類型枚舉
     */
    export enum TypeCallBack2AutoGroupResLoar{
        /**構(gòu)建失敗*/
        CREATE_GROUP_ERROR = 0,
        /**組加載完成*/
        GROUP_COMPLETE = 1,
        /**組加載進(jìn)度*/
        GROUP_PROGRESS = 2,
        /**組中元素加載失敗*/
        ITEM_LOAD_ERROR = 3
    }
}

測試調(diào)用:

module app {
   export class FntView extends eui.Component implements eui.UIComponent{
      private group_number : eui.Group;
      private img_webp : eui.Image;
      public constructor() {
         super();
         this.skinName = "resource/eui_skins/FntView.exml";
      }
      protected childrenCreated():void{
         super.childrenCreated();

         this.showNumer( 1139 );

         //this.img_webp.source = RES.getRes("bg_jpg");

            var texture:egret.Texture = RES.getRes("aab_webp");
            var result: egret.Bitmap = new egret.Bitmap();
            result.texture = texture;
            result.width = 500;
            result.height = 500;

            this.addChild(result);

            this.loaderRES();
      }




      private showNumer( num : number ) : void{
         let show : string = num.toString();
         FntManager.showFnt( this.group_number , "" , show , 125 , 75  );
      }



      ////////////////////////  RES加載器的使用
      private loaderRES():void{
         let resData : IAutoGroupResLoarData = {
            _groupName : "test2create",
            _arr2Res : [
               "c_01_jpg",
               "c_02_jpg"
            ],
            _callBack : this.callBack2Res,
            _class : this
         };
         AutoGroupResLoardManager.Ins.startLoader( resData );
      }

      private callBack2Res( type : TypeCallBack2AutoGroupResLoar , $class : FntView = null ,  $e : RES.ResourceEvent = null ) : void{
         switch ( type ){
            case TypeCallBack2AutoGroupResLoar.GROUP_COMPLETE:
                    //加載完成
                    let img01: eui.Image = new eui.Image();
                    img01.source = RES.getRes("c_01_jpg");
                    let img02: eui.Image = new eui.Image();
                    img02.source = RES.getRes("c_02_jpg");
                    img01.x = 100;
                    img01.y = 300;
                    img02.x = 300;
                    img02.y = 300;
                    $class.addChild(img01);
                    $class.addChild(img02);
               break;
            case TypeCallBack2AutoGroupResLoar.CREATE_GROUP_ERROR:
               console.error("創(chuàng)建組 : test2create 失敗");
               break;
            case TypeCallBack2AutoGroupResLoar.GROUP_PROGRESS:
               console.log(`進(jìn)度 : ${$e.itemsLoaded}/${$e.itemsTotal}`);//打印進(jìn)度
               break;
            case TypeCallBack2AutoGroupResLoar.ITEM_LOAD_ERROR:
               console.log(`自定義加載組 : ${$e.groupName} , 錯誤單元 : ${$e.resItem.url}`);
               break;
         }
      }
   }
}

結(jié)果:

Egret之RES資源加載

控制臺打印結(jié)果:

Egret之RES資源加載

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI