我们在开发中,不管是打的网络版还是本地版,或多或少都有可能加载一些网络上的资源,并且这些资源不想用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():vo