Laya LoaderManager灏忚

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laya LoaderManager灏忚相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e9%87%8d%e5%a4%8d' title='閲嶅'>閲嶅   type   灏辨槸   xtu   绛変簬   creat   custom   array   澧炲姞   

LoaderManager 绫荤敤浜庣敤浜庢壒閲忓姞杞借祫婧愩€傛绫绘槸鍗曚緥锛屼笉瑕佹墜鍔ㄥ疄渚嬪寲姝ょ被锛岃閫氳繃Laya.loader璁块棶銆傚叏閮ㄩ槦鍒楀姞杞藉畬鎴愶紝浼氭淳鍙?Event.COMPLETE 浜嬩欢锛涘鏋滈槦鍒椾腑浠绘剰涓€涓姞杞藉け璐ワ紝浼氭淳鍙?Event.ERROR 浜嬩欢锛屼簨浠跺洖璋冨弬鏁板€间负鍔犺浇鍑洪敊鐨勮祫婧愬湴鍧€銆?/p>

LoaderManager 绫绘彁渚涗簡浠ヤ笅鍑犵鍔熻兘锛?澶氱嚎绋嬶細榛樿5涓姞杞界嚎绋嬶紝鍙互閫氳繃maxLoader灞炴€т慨鏀圭嚎绋嬫暟閲忥紱 澶氫紭鍏堢骇锛氭湁0-4鍏?涓紭鍏堢骇锛屼紭鍏堢骇楂樼殑浼樺厛鍔犺浇銆?鏈€楂橈紝4鏈€浣庯紱 閲嶅杩囨护锛氳嚜鍔ㄨ繃婊ら噸澶嶅姞杞斤紙涓嶄細鏈夊涓浉鍚屽湴鍧€鐨勮祫婧愬悓鏃跺姞杞斤級浠ュ強澶嶇敤缂撳瓨璧勬簮锛岄槻姝㈤噸澶嶅姞杞斤紱 閿欒閲嶈瘯锛氳祫婧愬姞杞藉け璐ュ悗锛屼細閲嶈瘯鍔犺浇锛堜互鏈€浣庝紭鍏堢骇鎻掑叆鍔犺浇闃熷垪锛夛紝retryNum璁惧畾鍔犺浇澶辫触鍚庨噸璇曟鏁帮紝retryDelay璁惧畾鍔犺浇閲嶈瘯鐨勬椂闂撮棿闅斻€?/p>

LoaderManager鏄疞aya鍔犺浇璧勬簮鐨勭粺涓€鍏ュ彛锛岀鐞唋aya鐨勫姞杞藉櫒锛圠aya.Loader锛夛紝璐熻矗鎺у埗鍔犺浇浼樺厛绾э紝鍔犺浇绾跨▼缁存姢涓庝笂闄愪笂绾挎帶鍒讹紝鍔犺浇澶辫触鍚庨噸璇曠瓑鎿嶄綔銆傚湪laya涓娇鐢ㄧ殑Laya.loader鏄疞oaderManager鐨勫疄渚嬶紝鑰屼笉鏄疞aya.Loader鐨勫疄渚嬨€?/p>

婧愮爜闃呰

鍒濆鍖?/h5>
  • LoaderManager鍦↙aya.init鏃跺垱寤猴紝璧嬪€肩粰Laya.loader銆?/li>
  • 鎸夌収浼樺厛绾э紝鍒涘缓5锛坃maxPriority锛変釜闃熷垪锛岀敤浜庡瓨鏀鹃渶瑕佸姞杞界殑璧勬簮鏁版嵁銆傞槦鍒楃殑绱㈠紩灏辨槸鍔犺浇鐨勪紭鍏堢骇锛?-4锛屾暟瀛楄秺灏忥紝浼樺厛绾ц秺楂樸€傚姞杞芥椂锛屾寜鐓т紭鍏堢骇鍜岄槦鍒楅『搴忎緷娆″姞杞姐€?/li>
        /**
         * <p>鍒涘缓涓€涓柊鐨?<code>LoaderManager</code> 瀹炰緥銆?lt;/p>
         * <p><b>娉ㄦ剰锛?lt;/b>璇蜂娇鐢↙aya.loader鍔犺浇璧勬簮锛岃繖鏄竴涓崟渚嬶紝涓嶈鎵嬪姩瀹炰緥鍖栨绫伙紝鍚﹀垯浼氬鑷翠笉鍙鏂欑殑闂銆?lt;/p>
         */
        public function LoaderManager() {
            for (var i:int = 0; i < this._maxPriority; i++) this._resInfos[i] = [];is._resInfos[i] = [];
        }
鍗曚釜璧勬簮鍔犺浇娴佺▼
  • 鍒ゆ柇璧勬簮鏄惁瀛樺湪锛屽鏋滃瓨鍦ㄥ垯鐩存帴璋冪敤progress鍜宑omplete銆傚姞杞界殑璧勬簮瀛樻斁鍦↙aya.Loader涓紝涓嶅湪LoaderManager涓€?/li>
  • 鍒ゆ柇璧勬簮鏄惁鍦ㄥ姞杞介槦鍒椾腑锛?
    • 濡傛灉瀛樺湪锛屽垯鐩戝惉COMPLETE鍜孭ROFRESS浜嬩欢銆傛敞锛氭坊鍔犳椂涓嶈兘璁剧疆offBefore锛岄槻姝㈠悗缁姞杞界殑璧勬簮鐨勫洖璋冭鐩栦箣鍓嶇殑鍔犺浇瀹屾垚浜嬩欢銆?/li>
    • 濡傛灉涓嶅瓨鍦紝鍒欏垱寤篟esInfo锛屽皢info鏀惧埌瀵瑰簲浼樺厛绾ч槦鍒楃殑鏈熬锛岀洃鍚珻OMPLETE浜嬩欢銆傝皟鐢╪ext鏂规硶锛屽皾璇曞姞杞姐€?/li>
  • 妫€娴嬪綋鍓嶆鍦ㄤ娇鐢ㄧ殑loader鐨勬暟閲忥紝濡傛灉鏁伴噺涓嶈冻maxLoader涓暟锛屽垯鎸変紭鍏堢骇鏌ユ壘闃熷垪锛屽啀鍙栭槦鍒楃殑绗竴涓暟鎹幓鍔犺浇銆傚鏋滃綋鍓嶆鍦ㄤ娇鐢ㄧ殑loader鐨刢ount涓?锛屽垯瑙﹀彂Laya.loader锛圠oaderManager锛夌殑COMPLETE浜嬩欢銆?/li>
  • 鍔犺浇鏄娇鐢↙aya.Loader鎵ц鍔犺浇閫昏緫銆?/li>
  • 鍔犺浇瀹屾垚鍚庯紙鎴栬€呭け璐ワ級锛屽洖鏀跺綋鍓峫oader锛屾洿鏂發oader鏁伴噺锛屽鏋滃姞杞藉け璐ワ紝鍒欓噸鏂版斁鍏ュ姞杞介槦鍒楋紝骞朵笖浼樺厛绾ц缃负鏈€浣庛€傚鏋滃け璐ュ娆★紝鍒欏嚭鍙慐rror浜嬩欢锛屽皢url浼犻€掑嚭鍘汇€傚姞杞芥垚鍔熸垨鑰呭け璐ラ兘浼氬嚭鍙慍OMPLETE浜嬩欢锛岃皟鐢╟omplete鏂规硶銆傛竻鐞嗗拰鍥炴敹鐩稿叧鏁版嵁銆?/li>
  • 灏濊瘯璋冪敤next锛屽皾璇曢€夋嫨涓嬩竴涓祫婧愬姞杞姐€?/li>
        /**
         * <p>鍔犺浇璧勬簮銆傝祫婧愬姞杞介敊璇椂锛屾湰瀵硅薄浼氭淳鍙?Event.ERROR 浜嬩欢锛屼簨浠跺洖璋冨弬鏁板€间负鍔犺浇鍑洪敊鐨勮祫婧愬湴鍧€銆?lt;/p>
         * <p>鍥犱负杩斿洖鍊间负 LoaderManager 瀵硅薄鏈韩锛屾墍浠ュ彲浠ヤ娇鐢ㄥ涓嬭娉曪細Laya.loader.load(...).load(...);</p>
         * @param   url         瑕佸姞杞界殑鍗曚釜璧勬簮鍦板潃鎴栬祫婧愪俊鎭暟缁勩€傛瘮濡傦細绠€鍗曟暟缁勶細["a.png","b.png"]锛涘鏉傛暟缁刐{url:"a.png",type:Loader.IMAGE,size:100,priority:1},{url:"b.json",type:Loader.JSON,size:50,priority:1}]銆?         * @param   complete    鍔犺浇缁撴潫鍥炶皟銆傛牴鎹畊rl绫诲瀷涓嶅悓鍒嗕负2绉嶆儏鍐碉細1. url涓篠tring绫诲瀷锛屼篃灏辨槸鍗曚釜璧勬簮鍦板潃锛屽鏋滃姞杞芥垚鍔燂紝鍒欏洖璋冨弬鏁板€间负鍔犺浇瀹屾垚鐨勮祫婧愶紝鍚﹀垯涓簄ull锛?. url涓烘暟缁勭被鍨嬶紝鎸囧畾浜嗕竴缁勮鍔犺浇鐨勮祫婧愶紝濡傛灉鍏ㄩ儴鍔犺浇鎴愬姛锛屽垯鍥炶皟鍙傛暟鍊间负true锛屽惁鍒欎负false銆?         * @param   progress    鍔犺浇杩涘害鍥炶皟銆傚洖璋冨弬鏁板€间负褰撳墠璧勬簮鐨勫姞杞借繘搴︿俊鎭?0-1)銆?         * @param   type        璧勬簮绫诲瀷銆傛瘮濡傦細Loader.IMAGE銆?         * @param   priority    (default = 1)鍔犺浇鐨勪紭鍏堢骇锛屼紭鍏堢骇楂樼殑浼樺厛鍔犺浇銆傛湁0-4鍏?涓紭鍏堢骇锛?鏈€楂橈紝4鏈€浣庛€?         * @param   cache       鏄惁缂撳瓨鍔犺浇缁撴灉銆?         * @param   group       鍒嗙粍锛屾柟渚垮璧勬簮杩涜绠$悊銆?         * @param   ignoreCache 鏄惁蹇界暐缂撳瓨锛屽己鍒堕噸鏂板姞杞姐€?         * @return 姝?LoaderManager 瀵硅薄鏈韩銆?         */
        public function load(url:*, complete:Handler = null, progress:Handler = null, type:String = null, priority:int = 1, cache:Boolean = true, group:String = null, ignoreCache:Boolean = false):LoaderManager {
            if (url is Array) return _loadAssets(url as Array, complete, progress, type, priority, cache, group);
            var content:* = Loader.getRes(url);
            if (content != null) {
                //澧炲姞寤惰繜鍥炴帀
                Laya.timer.frameOnce(1, null, function():void {
                    progress && progress.runWith(1);
                    complete && complete.runWith(content);
                    //鍒ゆ柇鏄惁鍏ㄩ儴鍔犺浇锛屽鏋滄槸鍒欐姏鍑篶omplete浜嬩欢
                    _loaderCount || event(Event.COMPLETE);
                });
            } else {
                var info:ResInfo = _resMap[url];
                if (!info) {
                    info = _infoPool.length ? _infoPool.pop() : new ResInfo();
                    info.url = url;
                    info.type = type;
                    info.cache = cache;
                    info.group = group;
                    info.ignoreCache = ignoreCache;
                    //@鏀跺埌ResInfo鐨凜OMPLETE浜嬩欢鍒欐墽琛宑omplete鏂规硶
                    complete && info.on(Event.COMPLETE, complete.caller, complete.method, complete.args);
                    progress && info.on(Event.PROGRESS, progress.caller, progress.method, progress.args);
                    _resMap[url] = info;
                    //@閫夋嫨瀵瑰簲浼樺厛绾ч槦鍒?                    priority = priority < this._maxPriority ? priority : this._maxPriority - 1;
                    this._resInfos[priority].push(info);
                    _next();
                } else {
                    //@濡傛灉宸茬粡鍦ㄥ姞杞介槦鍒楋紝鍒欏彧娉ㄥ唽浜嬩欢锛屼笉鍒涘缓鍔犺浇鏁版嵁銆?                    //@娉?杩欓噷offBefore璁剧疆涓篺alse锛岄槻姝簨浠惰瑕嗙洊锛屽鑷碿omplete鎵ц涓嶅叏銆?                    complete && info._createListener(Event.COMPLETE, complete.caller, complete.method, complete.args, false, false);
                    progress && info._createListener(Event.PROGRESS, progress.caller, progress.method, progress.args, false, false);
                }
            }
            return this;
        }
        
        private function _next():void {
            //@loader婊′簡鍒欒繑鍥炵瓑寰呯┖闂瞝oader
            if (this._loaderCount >= this.maxLoader) return;
            //@鎸変紭鍏堢骇鏌ユ壘鍒楄〃
            for (var i:int = 0; i < this._maxPriority; i++) {
                var infos:Array = this._resInfos[i];
                while (infos.length > 0) {
                    var info:ResInfo = infos.shift();
                    if (info) return _doLoad(info);
                }
            }
            //@鎵€鏈塴oader閮界┖闂诧紝鍒欒Е鍙慙oaderManager鐨勫姞杞藉畬鎴?            _loaderCount || event(Event.COMPLETE);
        }
        
        private function _doLoad(resInfo:ResInfo):void {
            this._loaderCount++;
            var loader:Loader = this._loaders.length ? this._loaders.pop() : new Loader();
            loader.on(Event.COMPLETE, null, onLoaded);
            loader.on(Event.PROGRESS, null, function(num:Number):void {
                resInfo.event(Event.PROGRESS, num);
            });
            loader.on(Event.ERROR, null, function(msg:*):void {
                onLoaded(null);
            });
            
            var _this:LoaderManager = this;
            function onLoaded(data:* = null):void {
                //@娓呯悊銆佸洖鏀秎oader
                loader.offAll();
                loader._data = null;
                loader._customParse = false;
                _this._loaders.push(loader);
                _this._endLoad(resInfo, data is Array ? [data] : data);
                _this._loaderCount--;
                _this._next();
            }
            //@鍏蜂綋鍔犺浇娴佺▼鍦↙oader涓疄鐜?            loader._class = resInfo.clas;
            loader.load(resInfo.url, resInfo.type, resInfo.cache, resInfo.group, resInfo.ignoreCache);
        }
        
        private function _endLoad(resInfo:ResInfo, content:*):void {
            //濡傛灉鍔犺浇鍚庝负绌猴紝鏀惧叆闃熷垪鏈熬閲嶈瘯
            var url:String = resInfo.url;
            if (content == null) {
                var errorCount:int = this._failRes[url] || 0;
                //@璁板綍澶辫触娆℃暟
                if (errorCount < this.retryNum) {
                    console.warn("[warn]Retry to load:", url);
                    this._failRes[url] = errorCount + 1;
                    //@閲嶆柊鏀惧洖鍔犺浇闃熷垪
                    Laya.timer.once(retryDelay, this, _addReTry, [resInfo], false);
                    return;
                } else {
                    //瓒呰繃retryNum鍒欑洿鎺ユ姏鍑篍RROR浜嬩欢
                    console.warn("[error]Failed to load:", url);
                    event(Event.ERROR, url);
                }
            }
            if (_failRes[url]) _failRes[url] = 0;
            delete _resMap[url];
            //@閫氱煡info鎵цcomplete鏂规硶
            resInfo.event(Event.COMPLETE, content);
            resInfo.offAll();
            _infoPool.push(resInfo);
        }
        
        private function _addReTry(resInfo:ResInfo):void {
            this._resInfos[this._maxPriority - 1].push(resInfo);
            _next();
        }
鍔犺浇澶氫釜璧勬簮娴佺▼

LoaderManager.load鏀寔浼犲叆绠€鍗曠被鍨嬫暟缁勶紝姣斿锛氱畝鍗曟暟缁勶細["a.png","b.png"]锛涘鏉傛暟缁刐{url:"a.png",type:Loader.IMAGE,size:100,priority:1},{url:"b.json",type:Loader.JSON,size:50,priority:1}]銆?/p>

  • 閬嶅巻鎵€鏈夋暟缁勶紝璁板綍鎬绘暟锛岀敤浜庤绠梡rogress銆傝褰曚笅杞界殑鎬绘暟銆?/li>
  • 鍒嗗埆璋冪敤load鏂规硶锛屽姞杞芥瘡涓€涓厓绱犮€?/li>
  • 姣忓姞杞藉畬涓€涓祫婧愶紝妫€鏌ュ姞杞界殑鏁伴噺鏄惁绛変簬鏈鐨勬€绘暟閲忥紝濡傛灉鏄紝鍒欐墽琛宑omplete鎿嶄綔銆?/li>
        /**
         * @private
         * 鍔犺浇鏁扮粍閲岄潰鐨勮祫婧愩€?         * @param arr 绠€鍗曪細["a.png","b.png"]锛屽鏉俒{url:"a.png",type:Loader.IMAGE,size:100,priority:1},{url:"b.json",type:Loader.JSON,size:50,priority:1}]*/
        private function _loadAssets(arr:Array, complete:Handler = null, progress:Handler = null, type:String = null, priority:int = 1, cache:Boolean = true, group:String = null):LoaderManager {
            var itemCount:int = arr.length;
            var loadedCount:int = 0;
            var totalSize:int = 0;
            var items:Array = [];
            var success:Boolean = true;
            for (var i:int = 0; i < itemCount; i++) {
                var item:Object = arr[i];
                if (item is String) item = {url: item, type: type, size: 1, priority: priority};
                if (!item.size) item.size = 1;
                item.progress = 0;
                totalSize += item.size;
                items.push(item);
                var progressHandler:* = progress ? Handler.create(null, loadProgress, [item], false) : null;
                var completeHandler:* = (complete || progress) ? Handler.create(null, loadComplete, [item]) : null;
                load(item.url, completeHandler, progressHandler, item.type, item.priority || 1, cache, item.group || group);
            }
            
            function loadComplete(item:Object, content:* = null):void {
                loadedCount++;
                item.progress = 1;
                if (!content) success = false;
                if (loadedCount === itemCount && complete) {
                    complete.runWith(success);
                }
            }
            
            function loadProgress(item:Object, value:Number):void {
                if (progress != null) {
                    item.progress = value;
                    var num:Number = 0;
                    for (var j:int = 0; j < items.length; j++) {
                        var item1:Object = items[j];
                        num += item1.size * item1.progress;
                    }
                    var v:Number = num / totalSize;
                    progress.runWith(v);
                }
            }
            return this;
        }
create鏂规硶

鏍规嵁clas绫诲瀷鍒涘缓涓€涓湭鍒濆鍖栬祫婧愮殑瀵硅薄锛岄殢鍚庤繘琛屽紓姝ュ姞杞斤紝璧勬簮鍔犺浇瀹屾垚鍚庯紝鍒濆鍖栧璞$殑璧勬簮锛屽苟閫氳繃姝ゅ璞℃淳鍙?Event.LOADED 浜嬩欢锛屼簨浠跺洖璋冨弬鏁板€间负姝ゅ璞℃湰韬€傚宓岃祫婧愮殑瀛愯祫婧愪細淇濈暀璧勬簮璺緞"?"鍚庣殑閮ㄥ垎銆?br> 濡傛灉url涓烘暟缁勶紝杩斿洖true锛涘惁鍒欒繑鍥炴寚瀹氱殑璧勬簮绫诲璞★紝鍙互閫氳繃渚﹀惉姝ゅ璞$殑 Event.LOADED 浜嬩欢鏉ュ垽鏂祫婧愭槸鍚﹀凡缁忓姞杞藉畬姣曘€?br> 娉ㄦ剰锛歝ache鍙傛暟鍙兘瀵规枃浠跺悗缂€涓篴tlas鐨勮祫婧愯繘琛岀紦瀛樻帶鍒讹紝鍏朵粬璧勬簮浼氬拷鐣ョ紦瀛橈紝寮哄埗閲嶆柊鍔犺浇銆?/p>

  • create鏂规硶鍙互鍏堝垱寤轰竴涓寚瀹氱被鍨嬬殑澹筹紝鍚屾杩斿洖缁欒皟鐢ㄨ€呫€傚3閲岀殑璧勬簮鍒欓€氳繃寮傛鐨勬柟寮忓姞杞姐€?/li>
  • create鏂规硶涓昏鏄湪Laya3D涓娇鐢紝涓昏鏄疞aya鍐呴儴缁勪欢浣跨敤銆備娇鐢╟reate鍒涘缓鐨勭被鍨嬫湁涓ユ牸闄愬埗锛屽湪2D涓嬫敮鎸丄tlas锛屽湪3D鏈変竴瀹氭墿灞曘€?/li>
  • 杩欎簺绫诲瀷閮借瀹炵幇ICreateResource鏂规硶锛屾彁渚沷nAsynLoaded锛宊setUrl绛夋柟娉曘€傚鏋滃垱寤轰竴浜涗笉鏀寔鐨勭被鍨嬶紝浼氱洿鎺ユ姏鍑哄紓甯搞€?/li>
  • 鍚宭oad鏂规硶涓€鏍凤紝鏀寔浼犲叆涓€涓垨鑰呭涓祫婧愬湴鍧€銆?/li>
  • 浣跨敤create鏂规硶鍔犺浇Atlas璧勬簮鏃讹紝浼氳繑鍥瀗ull銆?/p>

  • Laya3D鎵€鏀寔鐨勭被鍨嬶細
    //鍦↙aya3D鍒濆鍖栨椂璁剧疆
    var createMap:Object = LoaderManager.createMap;
    createMap["lh"] = [Sprite3D, Laya3D.HIERARCHY];
    createMap["ls"] = [Scene, Laya3D.HIERARCHY];
    createMap["lm"] = [Mesh, Laya3D.MESH];
    createMap["lmat"] = [StandardMaterial, Laya3D.MATERIAL];
    createMap["lpbr"] = [PBRMaterial, Laya3D.MATERIAL];
    createMap["ltc"] = [TextureCube, Laya3D.TEXTURECUBE];
    createMap["jpg"] = [Texture2D, "nativeimage"];
    createMap["jpeg"] = [Texture2D, "nativeimage"];
    createMap["png"] = [Texture2D, "nativeimage"];
    createMap["pkm"] = [Texture2D, Loader.BUFFER];
    createMap["lsani"] = [AnimationTemplet, Loader.BUFFER];
    createMap["lrani"] = [AnimationTemplet, Loader.BUFFER];
    createMap["raw"] = [DataTexture2D, Loader.BUFFER];
    createMap["mipmaps"] = [DataTexture2D, Loader.BUFFER];
    createMap["thdata"] = [TerrainHeightData, Loader.BUFFER];
    createMap["lt"] = [TerrainRes, Laya3D.TERRAIN];
    createMap["lani"] = [AnimationClip, Loader.BUFFER];
    createMap["lav"] = [Avatar, Loader.JSON];
    createMap["ani"] = [AnimationTemplet, Loader.BUFFER];//鍏煎鎺ュ彛              

以上是关于Laya LoaderManager灏忚的主要内容,如果未能解决你的问题,请参考以下文章

用于在多个活动/片段中重用的全局加载器 (LoaderManager)

当我从 TabFragment 使用 LoaderManager 启动片段时,为啥 Tablayout 和 Viewpager 会因 NullpointerException 而崩溃?

LoaderManager 的范围是啥?

你可以使用服务中的 LoaderManager 吗?

LayaAir之小游戏开放域开发

带 LoaderManager 的 CursorLoader 如何知道将光标发送到 CursorAdapter?