我们在开发中,不管是打的网络版还是本地版,或多或少都有可能加载一些网络上的资源,并且这些资源不想用dcc方式,毕竟现在苹果对热更新管得比较严,那如果不用dcc方式,我们在Native上面的资源用Loader去加载后,并不会缓存到本地,这样会造成网络流量的浪费。
经过我对Laya生成的Native中的js脚本进行分析,以及多次尝试才做出来,而官方很不想帮忙回答这些问题,一问问题就是让你交钱成VIP。
下面是我的代码:
private function downloadFile(url:String,relativePath:String,onComplete:Handler,onError:Handler=null):void{
if(window.conch)
{
var folder:String=window.conch.getCachePath()+"/";
var relativeFolder:String = folder+relativePath.substring(0,relativePath.lastIndexOf("/"));
if (!window.fs_exists(relativeFolder)) {
window.fs_mkdir(relativeFolder);
}
var cachepath:String = folder+relativePath;
//判断文件是否存在
if (window.fs_exists(cachepath)) {
onComplete && onComplete.runWith("file://"+cachepath.replace(new RegExp('//','g'),"/"));
return;
}
var f:* = new window.conch_File(url);
var fr:* = new window.conch_FileReader();
fr.setIgnoreError(true);
fr.onload = function():void {
if(fr.result){
window.fs_writeFileSync(cachepath, fr.result);
onComplete && onComplete.runWith("file://"+cachepath.replace(new RegExp('//','g'),"/"));
}
else
{
onError && onError.runWith("no data");
}
};
// fr.onprogress = onprog;
fr.onerror = function(e):void{
onError && onError.runWith(e);
};
fr.readAsArrayBuffer(f);
}
else
{
onComplete && onComplete.runWith(url);
}
}
使用的代码:
var url:String="https://xxxxxxxx.xxx.xxx/2_3/2_3.png";
downloadFile(url,"2_3/2_3.png",Handler.create(this,function(localPath:String):void{
Laya.loader.load(localPath,Handler.create(this,function(t:Texture):void{
url="https://xxxxxxxx.xxx.xxx/2_3/2_3.sk";
downloadFile(url,"2_3/2_3.sk",Handler.create(this,function(localPath:String):void{
var ske:Skeleton = new Skeleton();
ske.url=localPath;
ske.pos(300,300);
Laya.stage.addChild(ske);
}));
}));
}));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)