LayaAr 批量加载资源 LoaderManager

目录

LoaderManager 

编码示例

资源卸载


LoaderManager 

Package laya.net
public class LoaderManager
Inheritance LoaderManager InheritanceEventDispatcher Inheritance Object

1、LoaderManager 类用于批量加载资源,此类是单例,不要手动实例化此类,通过 Laya.loader 访问

2、全部队列加载完成,会派发 Event.COMPLETE 事件;如果队列中任意一个加载失败,会派发 Event.ERROR 事件,事件回调参数值为加载出错的资源地址。

3、LoaderManager 类提供了以下几种功能:

1)多线程(默认5个加载线程),可以通过 maxLoader 属性修改线程数量;

2)多优先级:有0-4共5个优先级,优先级高的优先加载。0最高,4最低;

3)重复过滤:自动过滤重复加载(不会有多个相同地址的资源同时加载)以及复用缓存资源,防止重复加载;

4)错误重试:资源加载失败后,会重试加载(以最低优先级插入加载队列),retryNum 设定加载失败后重试次数,retryDelay 设定加载重试的时间间隔。

Property(属性)
maxLoader : int = 5 ,最大下载线程,默认为5个
retryDelay : int = 0 ,延迟时间多久再进行错误重试,默认立即重试
retryNum : int = 1 ,加载出错后的重试次数,默认重试一次
Method(方法)

cacheRes(url:String, data:*):void ,缓存资源。

cancelLoadByUrl(url:String):void ,根据地址清理掉未加载的内容

cancelLoadByUrls(urls:Array):void ,根据地址集合清理掉未加载的内容

clearRes(url:String):void ,清理指定资源地址缓存。

clearResByGroup(group:String):void ,根据分组清理资源。

clearTextureRes(url:String):void

销毁Texture使用的图片资源,保留texture壳,如果下次渲染的时候,发现texture使用的图片资源不存在,则会自动恢复 相比clearRes,clearTextureRes只是清理texture里面使用的图片资源,并不销毁texture,再次使用到的时候会自动恢复图片资源 而clearRes会彻底销毁texture,导致不能再使用;clearTextureRes能确保立即销毁图片资源,并且不用担心销毁错误,clearRes则采用引用计数方式销毁 【注意】如果图片本身在自动合集里面(默认图片小于51212),内存是不能被销毁的,此图片被大图合集管理器管理

clearUnLoaded():void ,清理当前未完成的加载,所有未加载的内容全部停止加载。

create(url:*, complete:Handler = null, progress:Handler = null, type:String = null, constructParams:Array = null, propertyParams:Object = null, priority:int = 1, cache:Boolean = true):void

根据clas类型创建一个未初始化资源的对象,随后进行异步加载,资源加载完成后,初始化对象的资源,并通过此对象派发 Event.LOADED 事件,事件回调参数值为此对象本身。套嵌资源的子资源会保留资源路径"?"后的部分。 如果url为数组,返回true;否则返回指定的资源类对象,可以通过侦听此对象的 Event.LOADED 事件来判断资源是否已经加载完毕。 注意:cache参数只能对文件后缀为atlas的资源进行缓存控制,其他资源会忽略缓存,强制重新加载。

getRes(url:String):* ,获取指定资源地址的资源。

load(url:*, complete:Handler = null, progress:Handler = null, type:String = null, priority:int = 1, cache:Boolean = true, group:String = null, ignoreCache:Boolean = false, useWorkerLoader:Boolean = false):LoaderManager

加载资源。资源加载错误时,本对象会派发 Event.ERROR 事件,事件回调参数值为加载出错的资源地址。 因为返回值为 LoaderManager 对象本身,所以可以使用如下语法:loaderManager.load(...).load(...);

setGroup(url:String, group:String):void ,设置资源分组。

更多 API 参考官网:https://layaair.ldc.layabox.com/api2/Chinese/index.html?category=Core&class=laya.net.LoaderManager

编码示例

通常游戏开始的时候,都需要先加载必要的资源,如上所示这个简易版的实现代码如下:

//初始化引擎
Laya.init(1000, 500, Laya.WebGL);
/**显示性能统计信息 */
Laya.Stat.show(0, 0);
var label_info;

const imgUrls = [];
imgUrls.push("http://tomcat.apache.org/res/images/tomcat.png");
imgUrls.push("http://www.apache.org/img/asf_logo.png");
imgUrls.push("https://cn.vuejs.org/images/logo.png?_sw-precache=cf23526f451784ff137f161b8fe18d5a");
imgUrls.push("http://img02.tuke88.com/preview_music/00/08/56/preview-5b835eaf13a9b9053.mp3");

/**load 加载应用中将使用到的资源列表
 * 第1个参表示要加载的单个资源地址或资源信息数组,比如:简单数组:["a.png","b.png"]
 * 第2个参数加载结束回调
 * 第3个参数是加载进度回调,回调参数值为当前资源的加载进度信息(0-1),Handler 要反复回调,所以要设置为 false,否则执行一次就会自动删除的
 */
Laya.loader.retryNum = 2;//加载出错后的重试次数,默认重试一次
Laya.loader.load(imgUrls,
    Laya.Handler.create(this, loadComplete),
    Laya.Handler.create(this, loadProgress, [], false));
showInfo();

/** 资源加载完成时回调*/
function loadComplete(isSuccess) {
    console.log("资源加载完成...", isSuccess);
    label_info.removeSelf();/**加载完成后从父容器中删除标签 label_info */
    var sprite = new Laya.Sprite();
    sprite.graphics.drawTexture(Laya.loader.getRes(imgUrls[0]), 200);
    Laya.stage.addChild(sprite);
}

/**资源加载过程中的进度回调
 * progress 取值 0-1 */
function loadProgress(progress) {
    console.log("当前加载资源:" + progress);
    label_info.text = (progress * 100) + "%";
}

/**显示标签信息 */
function showInfo() {
    label_info = new Laya.Label();
    label_info.fontSize = 20;
    label_info.color = "#fff";
    label_info.pos(200, 50);
    Laya.stage.addChild(label_info);
}

资源卸载

1、游戏运行时总会加载许多资源,这些资源在使用完成后应及时卸载,否则一直残留在内存中。

2、这是内存优化的常用操作之一,可以参考官网:https://ldc.layabox.com/doc/?nav=zh-js-3-2-1

/**
 * 清理指定资源地址缓存。
 * @param   url 资源地址。
 */
clearRes(url: string): void;

如上所示加载两张图片资源并在控制台打印加载进度,加载完成后,在舞台上显示第一张图片,当点击 Label 文本后,清理加载完成的所有资源,则缓存的所有资源无法再使用,正在显示的图片也消失了,实现代码如下:

//初始化引擎
Laya.init(1000, 500, Laya.WebGL);
/**显示性能统计信息 */
Laya.Stat.show(0, 0);
var label_info;

/**Laya.loader.load 当资源加载相同时,并不会重复加载,所以数组的大小仍然为4,但实际资源只有两份*/
const imgUrls = [];
for (var i = 0; i < 2; i++) {
	imgUrls.push("http://tomcat.apache.org/res/images/tomcat.png");
	imgUrls.push("http://www.apache.org/img/asf_logo.png");
}

/**load 加载应用中将使用到的资源列表
 * 第1个参表示要加载的单个资源地址或资源信息数组,比如:简单数组:["a.png","b.png"]
 * 第2个参数加载结束回调
 * 第3个参数是加载进度回调,回调参数值为当前资源的加载进度信息(0-1),Handler 要反复回调,所以要设置为 false,否则执行一次就会自动删除的
 * LoaderManager 会自动过滤重复加载(不会有多个相同地址的资源同时加载)以及复用缓存资源,防止重复加载
 */
Laya.loader.retryNum = 2;//加载出错后的重试次数,默认重试一次
Laya.loader.load(imgUrls,
	Laya.Handler.create(this, loadComplete),
	Laya.Handler.create(this, loadProgress, [], false));
showInfo();

/** 资源加载完成时回调*/
function loadComplete() {
	console.log("资源加载完成...", imgUrls.length);
	var sprite = new Laya.Sprite();
	sprite.graphics.loadImage(imgUrls[0], 200, 10);
	Laya.stage.addChild(sprite);

	label_info.on(Laya.Event.CLICK, this, function () {
		for (var i = 0, len = imgUrls.length; i < len; ++i) {
			var imgUrl = imgUrls[i];
			//清理前:资源存在并可以使用
			console.log("清理前:", Laya.loader.getRes(imgUrl));
			//clearRes:清理指定资源地址缓存
			Laya.loader.clearRes(imgUrl);
			//清理后:资源被卸载无法再使用,已经在使用的也会消失
			console.log("清理后", Laya.loader.getRes(imgUrl));
		}
	});
}

/**显示标签信息 */
function showInfo() {
	label_info = new Laya.Label("0%");
	label_info.fontSize = 20;
	label_info.color = "#fff";
	label_info.pos(400, 50);
	Laya.stage.addChild(label_info);
}

/**资源加载过程中的进度回调
 * progress 取值 0-1 */
function loadProgress(progress) {
	console.log("当前加载资源:" + progress);
	label_info.text = parseInt(progress * 100) + "%";
}

猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/85987981
今日推荐